least<T> function

T? least<T>(
  1. Iterable<T> iterable, [
  2. num comparator(
    1. T,
    2. T
    ) = ascending
])

Returns the least of all values in the iterable according to the specified comparator.

If the given iterable contains no comparable values (i.e., the comparator returns double.nan when comparing each value to itself), returns null.

If comparator is not specified, it defaults to ascending. For example:

const array = [
  {"foo": 42},
  {"foo": 91}
];
least(array, (a, b) => a["foo"]! - b["foo"]!); // {foo: 42}
least(array, (a, b) => b["foo"]! - a["foo"]!); // {foo: 91}

This function is similar to min, except that it accepts an iterable of values ​​that do not implement the Comparable interface.

Implementation

T? least<T>(Iterable<T> iterable, [num Function(T, T) comparator = ascending]) {
  T? max;
  var defined = false;
  for (final value in iterable) {
    if (defined
        ? comparator(value, max as T) < 0
        : comparator(value, value) == 0) {
      max = value;
      defined = true;
    }
  }
  return max;
}