minBy method

T? minBy(
  1. Comparator<T> comparator
)

Returns the minimal value based on the comparator function. If collection is empty this returns null.

Example:

[1, 0, 2].minBy((a, b) => a.compareTo(b));       // 0
persons.minBy((a, b) => a.age.compareTo(b.age)); // the youngest person

Implementation

T? minBy(Comparator<T> comparator) {
  if (isEmpty) {
    return null;
  }
  return reduce(
      (value, element) => comparator(value, element) < 0 ? value : element);
}