How to sort object by Date in Dart

142 Views Asked by At

Want to sort this object by date in dart enter image description here

I try use build in method .sort but it's not correct

1

There are 1 best solutions below

0
On BEST ANSWER

For this you have to convert date string into Datetime.

To sort in ascending order you can use:

list_name.sort((a, b) => (DateFormat('yyyy-MMM').parse(a).compareTo(DateFormat('yyyy-MMM').parse(b))));

To sort in descending order use - in this:

l.sort((a, b) => -(DateFormat('yyyy-MMM').parse(a).compareTo(DateFormat('yyyy-MMM').parse(b))));