isSortedByCompare<K> method
Null safety
- K keyOf(
- T element
- 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;
}