max<T> function
Returns the greater of a
and b
.
If by
is given, the produced Comparable is used. Otherwise a
and b
must be Comparable
s.
This function is unstable, either a
or b
may be returned if both are equal. Unlike math.max, it works on all
Comparable
s.
max(('a', 1), ('a', 2), by: (e) => e.$2); // ('a', 2)
max(1, 2); // 2
Implementation
@Possible({TypeError})
@useResult T max<T>(T a, T b, {Select<T, Comparable<Object>>? by}) => switch (by) {
_? => by(a).compareTo(by(b)) > 0 ? a : b,
_ => (a as Comparable).compareTo(b) > 0 ? a : b,
};