18 lines
434 B
C#
18 lines
434 B
C#
public static class IEnumerableExtensions {
|
|
public static T MinBy<T, K>(this IEnumerable<T> source, Func<T, K> selector) {
|
|
T min = default;
|
|
|
|
bool anyValue = false;
|
|
|
|
foreach (var item in source) {
|
|
if (!anyValue || Comparer<K>.Default.Compare(selector(item), selector(min)) < 0) {
|
|
min = item;
|
|
anyValue = true;
|
|
}
|
|
}
|
|
|
|
return min;
|
|
}
|
|
}
|
|
|