I’ve got a list of data which also contains ISO8601 DateTime format. How can I sort it by time not date?
[2022-12-06T22:37:49.292343,
2022-12-06T18:37:49.300045,
2022-12-06T15:37:49.307976]
I’ve got a list of data which also contains ISO8601 DateTime format. How can I sort it by time not date?
[2022-12-06T22:37:49.292343,
2022-12-06T18:37:49.300045,
2022-12-06T15:37:49.307976]
2
Answers
To sort the list of dates by their hour, you can use:
Output:
DateTime
objects.DateTime
objects that copy the times and that all use the same date.DateTime
objects.which prints:
Note that the above
sort
callback could be unnecessarily expensive for long lists since it could callDateTime.parse
multiple times on the sameString
s. It’d be better to convert yourList<String>
to aList<DateTime>
first and then sort that. Or, if you must start and end withString
s, you could use a Schwartzian transform.