sortBy<T, R> function
Sorting data
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));
}