How to order array of posts by timestamps?

Hi guys,

I wanted to know if there’s a way to get an array of posts be ordered by newest to oldest based off their timestamp. Right now I have an array that’s filled with posts created on different days and I want the array to be ordered from newest to oldest. The below shows an example of how the timestamps of the posts are store in MongoDB.

[February 28th 2023,  February 25th  2023, March 14  2023, February 2nd  2023,  April 24th  2023,  April 24th  2023, March 20th 2023,February 8th, 2023]

Thanks for the help

It’s being stored as a string. I am also using moment for the dates. Below is an example of how it looks in MongoDB Atlas

Timestamp: Febraury 18th, 2023 1:10:21 am             String

I would convert the strings to JavaScript dates and then sort the array using the sort method with an applicable compare function.

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