isSorted method Null safety

bool isSorted(
  1. [Comparator<T>? compare]
)

Whether the elements are sorted by the compare ordering.

If compare is omitted, it defaults to comparing the elements using their natural Comparable ordering.

Implementation

bool isSorted([Comparator<T>? compare]) {
  if (compare != null) {
    return IterableExtension(this).isSorted(compare);
  }
  var iterator = this.iterator;
  if (!iterator.moveNext()) return true;
  var previousElement = iterator.current;
  while (iterator.moveNext()) {
    var element = iterator.current;
    if (previousElement.compareTo(element) > 0) return false;
    previousElement = element;
  }
  return true;
}