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

Commit 596e6d5

Browse files
committed
Add MinBy() and MaxBy() implementation
1 parent 60984a8 commit 596e6d5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>3.2.1</Version>
3+
<Version>3.3.0</Version>
44
</PropertyGroup>
55

66
<PropertyGroup>

JustArchiNET.Madness/StaticExtensions21.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
// limitations under the License.
2121

2222
using System;
23+
using System.Collections.Generic;
2324
using System.IO;
25+
using System.Linq;
2426
using System.Security.Cryptography;
2527
using System.Text;
2628
using System.Threading.Tasks;
@@ -65,4 +67,30 @@ public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder buil
6567

6668
return builder;
6769
}
70+
71+
[MadnessType(EMadnessType.Implementation)]
72+
public static TSource? MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer = null) {
73+
if (source == null) {
74+
throw new ArgumentNullException(nameof(source));
75+
}
76+
77+
if (keySelector == null) {
78+
throw new ArgumentNullException(nameof(keySelector));
79+
}
80+
81+
return source.OrderByDescending(keySelector, comparer).FirstOrDefault();
82+
}
83+
84+
[MadnessType(EMadnessType.Implementation)]
85+
public static TSource? MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer = null) {
86+
if (source == null) {
87+
throw new ArgumentNullException(nameof(source));
88+
}
89+
90+
if (keySelector == null) {
91+
throw new ArgumentNullException(nameof(keySelector));
92+
}
93+
94+
return source.OrderBy(keySelector, comparer).FirstOrDefault();
95+
}
6896
}

0 commit comments

Comments
 (0)