maxOfWithOrNull<R> method

R? maxOfWithOrNull<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 or null if there are no elements.

Implementation

R? maxOfWithOrNull<R>(
  Comparator<R> comparator,
  R Function(E element) selector,
) {
  if (isEmpty) return null;

  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;
}