minInList<T> static method

T? minInList<T>(
  1. Iterable<T>? ns, [
  2. Comparator<T>? comparator
])

Minimal value of ns entries.

comparator is optional and useful for non num T.

Implementation

static T? minInList<T>(Iterable<T>? ns, [Comparator<T>? comparator]) {
  if (ns == null || ns.isEmpty) return null;

  if (comparator != null) {
    var min = ns.first;

    for (var n in ns) {
      if (comparator(n, min) < 0) min = n;
    }

    return min;
  } else {
    var min = ns.first as num;

    for (var n in ns.map((e) => e as num)) {
      if (n < min) min = n;
    }

    return min as T;
  }
}