maxInList<T> static method

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

Maximum value in ns entries.

comparator is optional and useful for non num T.

Implementation

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

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

    for (var n in ns) {
      if (comparator(n, max) > 0) max = n;
    }

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

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

    return max as T;
  }
}