Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting ascending and descending in Dart?

Tags:

sorting

dart

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?

like image 332
user3071121 Avatar asked Jan 12 '15 08:01

user3071121


People also ask

How do you sort descending in darts?

For descending When you swap the two arguments +1 becomes -1 and vice versa this leads to a descending order instead of an ascending one. print(nlist. sort((a, b) => a.

How do you rearrange lists in darts?

The core libraries in Dart are responsible for the existence of List class, its creation, and manipulation. Sorting of the list depends on the type of list we are sorting i.e. if we are sorting integer list then we can use simple sort function whereas if it is a string list then we use compareTo to sort the list.

How do you sort a DateTime Dart?

sort((a,b) => a. compareTo(b)); DateTime expiryAsDateTime = DateTime. parse(expiry);


1 Answers

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)); 
like image 96
Oluwafemi Tairu Avatar answered Oct 06 '22 12:10

Oluwafemi Tairu