Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Add MinBy() and MaxBy() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Feb 18, 2022
1 parent 60984a8 commit 596e6d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.2.1</Version>
<Version>3.3.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
28 changes: 28 additions & 0 deletions JustArchiNET.Madness/StaticExtensions21.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -65,4 +67,30 @@ public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder buil

return builder;
}

[MadnessType(EMadnessType.Implementation)]
public static TSource? MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer = null) {
if (source == null) {
throw new ArgumentNullException(nameof(source));
}

if (keySelector == null) {
throw new ArgumentNullException(nameof(keySelector));
}

return source.OrderByDescending(keySelector, comparer).FirstOrDefault();
}

[MadnessType(EMadnessType.Implementation)]
public static TSource? MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer = null) {
if (source == null) {
throw new ArgumentNullException(nameof(source));
}

if (keySelector == null) {
throw new ArgumentNullException(nameof(keySelector));
}

return source.OrderBy(keySelector, comparer).FirstOrDefault();
}
}

0 comments on commit 596e6d5

Please sign in to comment.