isSorted method

Coral<bool> isSorted([
  1. int compare(
    1. E a,
    2. E b
    )?
])

Returns true if this collection is sorted in ascending order.

Implementation

Coral<bool> isSorted([int Function(E a, E b)? compare]) =>
    coral.map((source) {
      if (source.length < 2) return true;
      final comp = compare ?? (a, b) => a.compareTo(b);
      var iterator = source.iterator;
      iterator.moveNext();
      var previous = iterator.current;
      while (iterator.moveNext()) {
        if (comp(previous, iterator.current) > 0) return false;
        previous = iterator.current;
      }
      return true;
    });