sortedByTime property

  1. @override
bool sortedByTime
override

Indicates whether the contents are sorted in increasing order of time value. If this is the case, time-based queries can be faster by performing a binary search. For every point in time only one entity is allowed to provide a location value, i.e. a list is not sorted if it contains two identical elements consecutively.

Implementation

@override
bool get sortedByTime {
  // If cached, return cached value.
  if (_sortedByTime != null) {
    return _sortedByTime!;
  }

  // Not cached -> determine state now. Start out assuming it's sorted,
  // then try to prove that's not the case.
  _sortedByTime = true;

  if (_collection.sortedByTime) {
    // If the wrapped collection is sorted by time, it's enough to check that
    // the indices list is in incremental order.
    for (var i = 1; i < _indices.length; i++) {
      if (_indices[i - 1] >= _indices[i]) {
        _sortedByTime = false;
        break;
      }
    }
  } else {
    // Wrapped collection is not sorted by time, but the indices may have
    // been provided in an order such that the result is sorted.
    for (var itemNr = 1; itemNr < length; itemNr++) {
      if (comparePointTimes(this[itemNr - 1], this[itemNr]) !=
          TimeComparisonResult.before) {
        _sortedByTime = false;
        break;
      }
    }
  }

  return _sortedByTime!;
}