maxE method

T maxE ([EqualityComparer<T> comparer ])

Returns the maximum value in the enumerable.

Iterates over the enumerable and applies the sorter property of the given EqualityComparer to each element in order to find the maximum value. Once iteration is complete, maxE will return the largest element found.

When the type of the enumerable is one of the below types, the EqualityComparer can be omitted. In this case, the function defaults to predefined maximum functions depending on the type:

  • Numeric types (num, int, double) return the maximum of all elements as defined by the max function in dart:math.
  • String types return the alphabetic maximum of all elements.

If the enumerable type is not one of these types, the EqualityComparer must be provided. Otherwise, an ArgumentError will be thrown.

If the enumerable is empty, an EmptyEnumerableError will be thrown.

The maxE function will iterate over every element in the enumerable.

Implementation

T maxE([EqualityComparer<T> comparer]) {
  IncompatibleTypeError.checkValidTypeOrParam(
      T, MaxReducers.director.keys, comparer);

  if (comparer == null) {
    return MaxReducers.director[T](this);
  }

  final iterator = this.iterator;
  if (!iterator.moveNext()) throw EnumerableError.isEmpty();

  T max;
  do {
    if (max == null)
      max = iterator.current;
    else if (comparer.sort(max, iterator.current) < 0) {
      max = iterator.current;
    }
  } while (iterator.moveNext());

  return max;
}