-3

If I have an array of objects, like:

[
{name: "First", updated: "2022-10-09"},
{name: "Second", updated: "null"},
{name: "Third", updated: "2022-12-06"}
]

Is there a way to sort in descending order by date without having to resort to creating date objects? And is there a way for it to filter out null values as well. I am not sure if the date as a string here qualifies as an ISO date and is readable as such - can it still be filtered as an ISO date?

So the final result would be:

[
{name: "Third", updated: "2022-12-06"}
{name: "First", updated: "2022-10-09"},
]
Joseph Walsh
  • 96
  • 1
  • 6

1 Answers1

1

You can just compare the string values like this:

const arr = [
{name: "First", updated: "2022-10-09"},
{name: "Second", updated: "2021-11-14"},
{name: "Third", updated: "2022-12-06"}
]
console.log(arr.sort((a, b) => a.updated > b.updated ? -1 : 1))
jessica-98
  • 609
  • 1
  • 6