maxOfWith<R> method

R maxOfWith<R>(
  1. Comparator<R> comparator,
  2. R selector(
    1. E element
    )
)

Returns the largest value according to the provided comparator among all values produced by selector function applied to each element in the collection.

Implementation

R maxOfWith<R>(Comparator<R> comparator, R Function(E element) selector) {
  if (isEmpty) throw NoSuchElementException();

  final iterator = this.iterator..moveNext();
  var maxValue = selector(iterator.current);

  while (iterator.moveNext()) {
    final value = selector(iterator.current);

    if (comparator(maxValue, value) < 0) maxValue = value;
  }

  return maxValue;
}