And I want to be able to sort by date. From most recent date to oldest date.
How can I go about this?
I’m guessing that first I would need to format the date, and the sort. But don’t know how to start, dates are kinda confusing since they come in so many formats.
I did short testing and apparently you can convert the string of date to an actual date object and then using the getTime method to retrieve its time in milliseconds since 1970. You could sort this number i assume
let date= new Date(`2021-03-14T16:41:45.577Z`)
console.log(date.getTime()) //1615740105577
i suppose you should be able to think your code from here
Just to be clear, the format of “2021-03-14T16:41:45.577Z” is an ISO 8601 datetime string. It is sortable as it because the longer durations come first and it’s military time. It goes year, month, day, hour (24-hour), minute, second, milliseconds. It doesn’t need to be formatted for sorting. The only problem would be if the dates were in different timezones, but the “Z” at the end means that it is “Zulu” or universal time, in other words Greenwich Mean Time.
That is what allows lasjorg’s answer to work. If you had it in some non-“biggest-to-smallest” format (e.g. “12/31/2020”) then that wouldn’t work and would need some kind of conversion to either a string that works (like the ISO string) or convert it to a numerical value like Sylvant is talking about. (Or would need a more complicated sorting function.)
But if you know that you will always be getting UTC ISO datetime strings, the lasjorg’s answer is what you need, just doing a simple sort and telling it off of which property to sort and how - in this case fairly simple.