sortItSelf<R extends Comparable> method

void sortItSelf<R extends Comparable>([
  1. R selector(
    1. T
    )?
])

Sorts this list in-place using the value returned by selector.

Example:

final nums = [3, 1, 5, 2];
nums.sortByInPlace((n) => n);
-> nums = [1, 2, 3, 5]

Implementation

void sortItSelf<R extends Comparable>([R Function(T)? selector]) {
  if (selector == null) {
    if (isEmpty || this.first is! Comparable) {
      throw StateError(
        "sortItSelf() without selector requires T to be Comparable.",
      );
    }
    (this as List<Comparable>).sort();
    return;
  }

  sort((a, b) => selector(a).compareTo(selector(b)));
}