-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

Troublegum
  • 31
  • 1
  • 5

1 Answers1

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);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48