minBy<R extends Comparable> method

E minBy<R extends Comparable>(
  1. R selector(
    1. E element
    )
)

Returns the first element yielding the smallest value of the given function. Throws StateError if there are no elements in the collection.

Implementation

E minBy<R extends Comparable<dynamic>>(R Function(E element) selector) {
  if (isEmpty) throw StateError('no elements');
  if (length == 1) return first;
  var minElement = first;
  var minValue = selector(minElement);
  for (final element in this) {
    final value = selector(element);
    if (minValue > value) {
      minValue = value;
      minElement = element;
    }
  }
  return minElement;
}