nextPermutation method
Permutes this List in-place into the next permutation with respect
to the provided comparator
. Returns true
if such a permutation
exists, otherwise leaves the list unmodified and returns false
.
Implementation
bool nextPermutation({Comparator<E>? comparator}) {
comparator ??= naturalCompare;
var i = length - 2;
while (i >= 0 && comparator(this[i], this[i + 1]) >= 0) {
i--;
}
if (i < 0) {
return false;
}
var j = length - 1;
while (comparator(this[i], this[j]) >= 0) {
j--;
}
swap(i, j);
reverseRange(i + 1, length);
return true;
}