isSorted method

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

Returns true if this collection is sorted in ascending order reactively.

Ensures:

  • Returns true if collection length is less than 2.
  • Uses compare comparator if provided, falling back to Comparable.compareTo.

Implementation

Bloom<bool> isSorted([int Function(E a, E b)? compare]) => computed((via) {
      final source = via(bloom);
      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;
    });