maxBy<S, T> function
Returns the element of values
for which orderBy
returns the maximum
value.
The values returned by orderBy
are compared using the compare
function.
If compare
is omitted, values must implement Comparable<T>
and they
are compared using their Comparable.compareTo.
Returns null
if values
is empty.
Implementation
S? maxBy<S, T>(Iterable<S> values, T Function(S) orderBy,
{int Function(T, T)? compare}) {
compare ??= defaultCompare;
S? maxValue;
T? maxOrderBy;
for (var element in values) {
var elementOrderBy = orderBy(element);
if (maxOrderBy == null || compare(elementOrderBy, maxOrderBy) > 0) {
maxValue = element;
maxOrderBy = elementOrderBy;
}
}
return maxValue;
}