How to format these dates and sort?

Hello there,

Let’s say I have this array with the following order

[
  {title: "World", date: "2021-03-14T16:41:45.577Z"},
  {title: "Hello", date: "2020-02-14T17:41:45.577Z"}
]

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.

Appreciate any help. Thanks in advance!

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

You can sort using date objects.

const arr = [
  { title: 'World', date: '2021-03-14T16:41:45.577Z' },
  { title: 'Hello', date: '2020-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2010-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2015-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:31:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:31:20.577Z' },
];

arr.sort((a, b) => new Date(b.date) - new Date(a.date));

console.log(arr);
/*
[ { title: 'World', date: '2021-03-14T16:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:41:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:31:45.577Z' },
  { title: 'Hello', date: '2021-02-14T12:31:20.577Z' },
  { title: 'Hello', date: '2020-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2015-02-14T17:41:45.577Z' },
  { title: 'Hello', date: '2010-02-14T17:41:45.577Z' } ]
*/

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.