sortBy<T, R> function Sorting data

List<T> sortBy<T, R>(
  1. Iterable<T> iterable,
  2. R accessor(
    1. T
    ), [
  3. num comparator(
    1. R,
    2. R
    ) = ascending
])

Returns a list containing the elements of the given iterable in the sorted order defined by the values yielded by the given accessor function.

If comparator is not specified, it defaults to ascending. Equivalent to List.sort, except that it does not mutate the given iterable.

sortBy(data, (d) => d["value"]);

it is equivalent to a comparator using natural order:

sort(data, (a, b) => ascending(a["value"], b["value"]));

The accessor is only invoked once per element, and thus the returned sorted order is consistent even if the accessor is nondeterministic.

Implementation

List<T> sortBy<T, R>(Iterable<T> iterable, R Function(T) accessor,
    [num Function(R, R) comparator = ascending]) {
  final index = Uint32List(iterable.length);
  for (var i = 0; i < iterable.length; i++) {
    index[i] = i;
  }
  var values = iterable.map(accessor), compare = compareDefined(comparator);
  index.sort((i, j) => compare(values.elementAt(i), values.elementAt(j)));
  return List.castFrom(permute(iterable, index));
}