isSortedBy<K extends Comparable<K>> method

bool isSortedBy<K extends Comparable<K>>(
  1. K keyOf(
    1. T element
    )
)

Whether the elements are sorted by their keyOf property.

Applies keyOf to each element in iteration order, then checks whether the results are in non-decreasing Comparable order.

Implementation

bool isSortedBy<K extends Comparable<K>>(K Function(T element) keyOf) {
  var iterator = this.iterator;
  if (!iterator.moveNext()) return true;
  var previousKey = keyOf(iterator.current);
  while (iterator.moveNext()) {
    var key = keyOf(iterator.current);
    if (previousKey.compareTo(key) > 0) return false;
    previousKey = key;
  }
  return true;
}