39

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?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
user3071121
  • 605
  • 2
  • 8
  • 12

6 Answers6

83

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));
Oluwafemi Tairu
  • 960
  • 1
  • 8
  • 12
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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 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 – user3071121 Jan 12 '15 at 08: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 '15 at 08:27
18

For Ascending:

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

For Descending:

var compare = (b, a) => -a.compareTo(b);
arc
  • 339
  • 3
  • 8
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]
Szczerski
  • 839
  • 11
  • 11
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.

Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
-1
var nlist = [1, 6, 8, 2, 16, 0];
nlist.sort((a, b) => a - b);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 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 '21 at 18:11