I have an array of objects so structured.
let array = [
{date: "22/03/2021 17:57", y: 10, type: "dil"},
{date: "22/03/2021 17:58", y: 1, type: "dil"},
{date: "15/04/2021 14:52", y: 3, type: "dil"},
{date: "24/03/2021 14:52", y: 4, type: "dil"},
{date: "01/04/2021 14:52", y: -2, type: "spp"},
{date: "24/03/2021 14:53", y: -5, type: "spp"},
{date: "18/04/2021 16:28", y: 3, type: "spp}
]
I have to sort it by date but I can't figure out how to do it because date is a string and if I use the sort method
array.sort((a,b) => (a.x > b.x) ? 1 : ((b.x > a.x) ? -1 : 0))
it gets ordered based on the first two characters, and not properly by confronting year, month, date, hour, and minutes.
Any ideas? I'm sure it is something very easy but I'm quite stuck.
toISOStringmethod on dates, it doesn't have a built-in format for european datetimes only RFC 2822 and ISO8601, and doesn't provide generic format strings, so you will have to parse the inputs by hand (or with something like moment.js).Stringjust for this one specific case, as in the accepted answer, is overkill...