isSortedByCompare<K> method Null safety

bool isSortedByCompare<K>(
  1. K keyOf(
    1. T element
    ),
  2. Comparator<K> compare
)

Whether the elements are compare-sorted by their keyOf property.

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

Implementation

bool isSortedByCompare<K>(
    K Function(T element) keyOf, Comparator<K> compare) {
  var iterator = this.iterator;
  if (!iterator.moveNext()) return true;
  var previousKey = keyOf(iterator.current);
  while (iterator.moveNext()) {
    var key = keyOf(iterator.current);
    if (compare(previousKey, key) > 0) return false;
    previousKey = key;
  }
  return true;
}