min<T> static method

T min<T>(
  1. Iterable<T> list, {
  2. Comparable by(
    1. T
    )?,
})

Returns the element of list with the smallest value of by (or the smallest element itself when by is null and T is Comparable).

Arr.min(users, by: (u) => u.age); // user with the youngest age

Implementation

static T min<T>(Iterable<T> list, {Comparable Function(T)? by}) {
  if (list.isEmpty) throw ArgumentError('list must not be empty');
  T? best;
  Comparable? bestKey;
  for (final v in list) {
    final k = by != null ? by(v) : (v as Comparable);
    if (bestKey == null || k.compareTo(bestKey) < 0) {
      bestKey = k;
      best = v;
    }
  }
  return best as T;
}