I see in the API docs there is a sort()
method on List
, but I'm not clear what it needs for a parameter. The current need is for a very simple straight up alpha comparison.
7 Answers
1. A Quick Solution
Thanks for the question! You can sort a list of String
s like this:
main() {
final List<String> fruits = <String>['bananas', 'apples', 'oranges'];
fruits.sort();
print(fruits);
}
The above code prints:
[apples, bananas, oranges]
2. Slightly more advanced usage
Notice that sort()
does not return a value. It sorts the list without creating a new list. If you want to sort and print in the same line, you can use method cascades:
print(fruits..sort());
For more control, you can define your own comparison logic. Here is an example of sorting the fruits based on price.
main() {
final List<String> fruits = <String>['bananas', 'apples', 'oranges'];
fruits.sort((a, b) => getPrice(a).compareTo(getPrice(b)));
print(fruits);
}
Let's see what's going on here.
A List
has a sort method, which has one optional parameter: a Comparator. A Comparator is a typedef
or function alias. In this case, it's an alias for a function that looks like:
int Comparator(T a, T b)
From the docs:
A Comparator function represents such a total ordering by returning a negative integer if a is smaller than b, zero if a is equal to b, and a positive integer if a is greater than b.
3. How to do it with a list of custom objects
Additionally, if you create a list composed of custom objects, you could add the Comparable<T>
as a mixin
or as inheritance (extends
) and then override the compareTo
method, in order to recreate the standard behavior of sort()
for your list of custom objects. For more info, do check out this other, related StackOverflow answer.

- 6,148
- 6
- 38
- 76

- 112,095
- 66
- 196
- 279
-
Thanks! Sorry to complain but while I am trying to appreciate the apparent versatility of the above, I do not find it intuitive or inline with previous language experience. Do you guys have somebody with a square wooden mouse and a long grey beard coming up with some of this stuff? :) – george koller Oct 15 '12 at 20:23
-
1This is a weird historical artifact. The comparator should be optional. It used to be, but we changed the optional parameter syntax a while back, which forced us to make all parameters non-optional for a while, including this one. We just haven't gotten a chance to fix this and make it optional again yet. – munificent Oct 15 '12 at 23:20
-
Thanks @munificent glad to hear it'll get fixed soon. See bug http://code.google.com/p/dart/issues/detail?id=1235 – Seth Ladd Oct 16 '12 at 03:30
-
I've updated the answer based on the new API. Much simpler now :) – Seth Ladd Oct 03 '13 at 07:42
Here is the one line code to achieve it.
fruits.sort((String a, String b)=>a.compareTo(b)); //fruits is of type List<String>

- 1,345
- 14
- 13
For Sorting Simple List of Integers or Strings:
var list = [5 , -5 ,1];
list.sort(); //-5 , 1 , 5
For Reversing the list order:
list.reversed;
For Sorting List of Objects or Map by field of it:
List<Map<String, dynamic>> list= [
{"name": "Shoes", "price": 100},
{"name": "Pants", "price": 50},
];
// from low to high according to price
list.sort((a, b) => a["price"].compareTo(b["price"]));
// from high to low according to price
list.sort((a, b) => b["price"].compareTo(a["price"]));

- 18,379
- 16
- 47
- 61

- 1,415
- 14
- 17
To add just one point to Seth's detailed answer, in general, in
(a, b) => foo(a, b)
passed into sort
, the function foo
should answer an integer result as follows:
- if a < b, result should be < 0,
- if a = b, result should be = 0, and
- if a > b, result should be > 0.
For the above law of trichotomy to hold, both a
and b
must be Comparable
s.

- 575
- 6
- 12
use compareAsciiUpperCase
instead of compareTo
, as it supports strings and automatically ignores case sensitive:
import "package:collection/collection.dart";
data.sort((a, b) {
return compareAsciiUpperCase(a.name, b.name);
});
After today, you should just be able to do list.sort() . The sort method's argument is now optional, and it defaults to a function that calls compareTo on the elements themselves. Since String is Comparable, it should Just Work now.

- 356
- 2
- 2
-
Was the List sort() method ever fixed to not require a comparator function? Or was there a regression? I'm using Dart 0.2.6.0_r15355 on Ubuntu 12.04 and getting the following results: [3,1,2].sort()==null and ['a','c','b'].sort()==null. – devdanke Dec 01 '12 at 21:48
-
Oops! Dart was sorting the array:-) But the sort() function returns void:-( So "print([3,1,2].sort())'" outputs "null". In this case, the Dart devs are not implementing a fluent programming style. I wish they would. Because then you could use the result of calling sort() immediately. – devdanke Dec 01 '12 at 23:25
How I have solved this problem.
List<Product> _dataSavingListProducts = [];
List<Product> _dataSavingListFavoritesProducts = [];
void _orderDataSavingLists() {
_dataSavingListProducts.toList().reversed;
_dataSavingListFavoritesProducts.toList().reversed;
}

- 748
- 6
- 14