checkContentsSortedByTime method
Checks whether the contents are sorted by increasing time in cases where the list is not marked as sortedByTime.
Even if the collection is not marked as sortedByTime, it might be
entirely or partially sorted (depending on how its contents were
initialized).
If sortedByTime is true, the method immediately returns true. Otherwise
it checks the contents (optionally skipping the first skipItems
items
and limiting itself to nrItems
rather than the entire list) to determine
if they are in fact sorted.
In the situation that the list is found to be fully sorted,
sortedByTime will be set to true as well.
Implementation
bool checkContentsSortedByTime([int skipItems = 0, int? nrItems]) {
if (sortedByTime) {
return true;
}
final endIndex = skipItems + (nrItems ?? (length - skipItems));
var detectedSorted = true;
for (var itemNr = skipItems + 1; itemNr < endIndex; itemNr++) {
switch (compareElementTime(itemNr - 1, itemNr)) {
case TimeComparisonResult.before:
break; // Breaks the swithc, not the for loop.
case TimeComparisonResult.same:
case TimeComparisonResult.after:
case TimeComparisonResult.overlapping:
detectedSorted = false;
break; // Breaks the switch, not the for loop.
}
// Stop the loop as soon as we find it's not sorted.
if (!detectedSorted) {
break;
}
}
// If we compared the entire list and found it's sorted, set the internal
// flag.
if (detectedSorted && skipItems == 0 && endIndex == length) {
_sortedByTime = true;
}
// Note that detectedSorted is not necessarily same as _sortedByTime,
// since the comparison may have skipped a number of items at the beginning!
return detectedSorted;
}