sort method Null safety
- [int compare(
- E a,
- E b
Sorts this list according to the order specified by the
compare function must act as a Comparator.
var numbers = ['two', 'three', 'four']; // Sort from shortest to longest. numbers.sort((a, b) => a.length.compareTo(b.length)); print(numbers); // [two, four, three]
List<int> nums = [13, 2, -11]; nums.sort(); print(nums); // [-11, 2, 13]
In that case, the elements of the list must be Comparable to each other.
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:
var 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]
external void sort([int compare(E a, E b)?]);