40

I have this

var nlist = [4,2,1,5];
var compare = (a, b) => a.compareTo(b);
nlist.sort(compare);
print(nlist);        // [1,2,4,5]

and here (where I changed the (b, a) to (a, b))

var nlist = [4,2,1,5]
var compare = (b, a) => a.compareTo(b);
nlist.sort(compare);
print(nlist);        // [5,4,2,1]

Why does this little modification change from ascending to descending order?

6 Answers 6

87

For ascending

var nlist = [1, 6, 8, 2, 16, 0];
nlist.sort((a, b) => a.compareTo(b));

For descending

var nlist = [1, 6, 8, 2, 16, 0];
nlist.sort((b, a) => a.compareTo(b));
0
33

Would be interesting what you actually expected from happening after the change.

Compare returns +1, 0, -1 depending on whether the 2nd argument is greater than the 1st or 0 if they are equal. When you swap the two arguments +1 becomes -1 and vice versa this leads to a descending order instead of an ascending one.

1.compareTo(2)

returns -1 and

print(nlist.sort((a, b) => a.compareTo(b)));

prints the list elements in ascending order, so yes, ascending is default.

2
  • Thanks @Günter Zöchbauer. But so, what's the "default" order it follow, +1 or -1? I mean in case (a, b) if it return 1 then I know a > b then it will put a before b or b before a Jan 12, 2015 at 8:52
  • 2
    The default order is (a, b)=>a.compareTo(b). This is the order you get if you provide no compare function to sort - it defaults to using the elements' own compareTo functions in this order. This is the default order for the types themselves, which is numeric order on numbers and lexicograpical order on strings. If you need to sort elements that are not Comparable, you can't fall back on compareTo, and have to write a function that returns negative if the first element is smaller than the second, positive if it's the other way around, and zero if the elements are equal.
    – lrn
    Jan 13, 2015 at 8:27
19

For Ascending:

var nlist = [4,2,1,5]
var compare = (b, a) => a.compareTo(b);

For Descending:

var compare = (b, a) => -a.compareTo(b);
1
  • 4
    @arc The answer is upside down. First one will give values in descending order and second one will give in ascending order. Feb 13, 2020 at 16:04
10

Another way to sort

  var nlist = [4,2,1,5];
  var ascending = nlist..sort();
  var descending = ascending.reversed;
  print(ascending);  // [1, 2, 4, 5]
  print(descending);  // [5, 4, 2, 1]
3
8

For ascending

There is no need to write list.sort((a, b) => a.compareTo(b));. This works:

list.sort();

For descending

list.sort((a, b) => b.compareTo(a));

Or, as of 2021, if you use https://pub.dev/packages/fast_immutable_collections (FIC package) this works:

list.sortReversed();

Notes:

  • (list..sort()).reversed does NOT reverse the list, in place. Instead, it sorts the original list in ascending order, and then returns a descending iterable, not a List (i.e., Iterable<int>, not List<int>). Maybe that's what you want. If not, you have to do list = (list..sort()).reversed.toList();. However, for large lists list.sortReversed() is much faster.

  • In the beforementioned FIC package there is also a reversedView() method, which returns a descending VIEW of the list. A "view" means if you later add items to the original list, the reversed one will also have those items, and vice-versa. So if your list is already sorted in ascending order and you want to have access to the descending list without wasting time doing the reversal, just do:

    list.sort();
    List<int> listDescending = list.reversedView; // Very fast.
    

Disclaimer: I am the author of the FIC package.

-1
var nlist = [1, 6, 8, 2, 16, 0];
nlist.sort((a, b) => a - b);
1
  • It is better to add some explanations to accompany your code, especially to such old question with many existing answers. Explanations will help readers not only solve their immediate problem by copy-pasting your code, but also to understand the code and be able to solve similar problems as well
    – Tomerikoo
    Dec 21, 2021 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.