Sorts this list according to the order specified by the compare
function.
The compare
function must act as a Comparator.
List<String> numbers = ['two', 'three', 'four']; // Sort from shortest to longest. numbers.sort((a, b) => a.length.compareTo(b.length)); print(numbers); // [two, four, three]
The default List implementations use Comparable.compare if compare
is omitted.
List<int> nums = [13, 2, -11]; nums.sort(); print(nums); // [-11, 2, 13]
A Comparator may compare objects as equal (return zero), even if they are distinct objects. The sort function is not guaranteed to be stable, so distinct objects that compare as equal may occur in any order in the result:
List<String> numbers = ['one', 'two', 'three', 'four']; numbers.sort((a, b) => a.length.compareTo(b.length)); print(numbers); // [one, two, four, three] OR [two, one, four, three]
void sort([int compare(E a, E b)]) { throw new UnsupportedError("Cannot sort immutable List."); }
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-html/ImmutableListMixin/sort.html