maxBy<R extends Comparable> method

T? maxBy<R extends Comparable>(
  1. R selector(
    1. T
    )
)

Returns the first element yielding the largest value of the given function or null if there are no elements.

Implementation

T? maxBy<R extends Comparable>(R Function(T) selector) {
  final i = iterator();
  if (!iterator().hasNext()) return null;
  T maxElement = i.next();
  R maxValue = selector(maxElement);
  while (i.hasNext()) {
    final e = i.next();
    final v = selector(e);
    if (maxValue.compareTo(v) < 0) {
      maxElement = e;
      maxValue = v;
    }
  }
  return maxElement;
}