-3

I have the following nested object. I would like to iterate through every item (latest.sentTime) and then sort the object itself by "sentTime". So that the most recent message is on top. How can I achieve this?

In the following example, "0" and "1" need to be swapped basically, since "1" has a more recent "sentTime".

enter image description here

1
  • yourArray.sort(({ latest: { sentTime: a } }, { latest: { sentTime: b } }) => b.localeCompare(a));. Commented Feb 6, 2022 at 13:29

1 Answer 1

2

const arr = [ 
  { latest: { sentTime: '2022-02-05T19:15:32.000Z' } },
  { latest: { sentTime: '2022-02-06T22:12:00.000Z' } } 
];

const sentTimes = arr
  .map(({ latest }) => latest.sentTime)
  .sort((a, b) => new Date(b) - new Date(a));

console.log(sentTimes);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.