isSorted method Null safety
- Comparator<
T> compare
Whether the elements are sorted by the compare
ordering.
Compares pairs of elements using compare
to check that
the elements of this iterable to check
that earlier elements always compare
smaller than or equal to later elements.
An single-element or empty iterable is trivially in sorted order.
Implementation
bool isSorted(Comparator<T> compare) {
var iterator = this.iterator;
if (!iterator.moveNext()) return true;
var previousElement = iterator.current;
while (iterator.moveNext()) {
var element = iterator.current;
if (compare(previousElement, element) > 0) return false;
previousElement = element;
}
return true;
}