sortItDesc<R extends Comparable> method

List<T> sortItDesc<R extends Comparable>([
  1. R selector(
    1. T
    )?
])

Returns a new list sorted in descending order based on selector.

Does NOT modify the original list.

Implementation

List<T> sortItDesc<R extends Comparable>([R Function(T)? selector]) {
  final copy = [...this];

  if (selector == null) {
    if (isEmpty || first is! Comparable) {
      throw StateError(
        "sortItDesc() without selector requires T to be Comparable.",
      );
    }
    (copy as List<Comparable>).sort((a, b) => b.compareTo(a));
    return copy;
  }

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