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?
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.
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.
sort((a,b) => a. compareTo(b)); DateTime expiryAsDateTime = DateTime. parse(expiry);
var nlist = [1, 6, 8, 2, 16, 0] nlist.sort((a, b) => a.compareTo(b));
var nlist = [1, 6, 8, 2, 16, 0] nlist.sort((b, a) => a.compareTo(b));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With