maxIndex<T extends Comparable?> function Summarizing data

int maxIndex<T extends Comparable?>(
  1. Iterable<T> iterable
)

Returns the index of the maximum of all values in the iterable.

The maximum is determined based on the natural ordering of the values, as defined by the Comparable interface.

This function ignores values that do not satisfy any of the following conditions:

  1. The value is not null.
  2. The value is not a special case like double.nan, which does not satisfy self-equality according to Object.==.

Useful for filtering and ignoring missing data in datasets.

If the iterable is empty or contains no valid values, this function returns -1.

Implementation

int maxIndex<T extends Comparable?>(Iterable<T> iterable) {
  T? max;
  var maxIndex = -1, index = -1;
  for (final value in iterable) {
    ++index;
    if (value == null || value != value) continue;
    if (max == null || max.compareTo(value) < 0) {
      max = value;
      maxIndex = index;
    }
  }
  return maxIndex;
}