sortedBy<T> function

List sortedBy<T>(
  1. Iterable<T> items,
  2. int sortKey(
    1. T
    ), {
  3. bool reverse = false,
})

Returns a copy of `items, stably sorted by sortKey.

The implementation uses collection.insertionSort, which is optimized for short lists.

Implementation

List sortedBy<T>(Iterable<T> items, int Function(T) sortKey,
    {bool reverse: false}) {
  final list = new List.from(items);
  final comparator = reverse
      ? (a, b) => sortKey(a) - sortKey(b)
      : (a, b) => sortKey(b) - sortKey(a);
  insertionSort(list, compare: comparator);
  return list;
}