const res = [
{
_id: '5fbfa729fc46a415ce5503a6',
first_name: 'AAA',
last_name: 'BBBB',
story: [
{
_id: '6136096f4255d84bcb4a7144',
timestamp: 1630931311227,
expire_at: '2021-09-06T12:28:31.227Z'
}
]
},
{
_id: '5fbf6f91aff7f3320a906547',
first_name: 'DDD',
last_name: 'FFFF',
story: [
{
_id: '613609414255d84bcb4a7122',
timestamp: 1630931265409,
expire_at: '2021-09-06T12:27:45.409Z'
}
]
},
{
_id: '5fbfa748fc46a415ce5503a8',
first_name: 'EEEE',
last_name: 'FFFF',
story: [
{
_id: '613609184255d84bcb4a710a',
timestamp: 1630931224383,
expire_at: '2021-09-06T12:27:04.383Z'
},
{
_id: '613709f49223350dfdaec618',
timestamp: 1630996980379,
expire_at: '2021-09-07T06:43:00.379Z'
},
{
_id: '61372d81c0a7ec4fa0f7a8e3',
timestamp: 1631006081890,
expire_at: '2021-09-07T09:14:41.890Z'
}
]
}
];
res.forEach((val, key) => {
console.log('val', val.story);
val.story.sort((a, b) =>
a.timestamp > b.timestamp ? 1 : b.timestamp > a.timestamp ? -1 : 0
);
});
I want to sort the object based on story's timestamp. who has posted latest story it should come first for example EEE has latest story post it should come at first place.
this object should be at first place after that others which false later this story and so on
{
_id: '5fbfa748fc46a415ce5503a8',
first_name: 'EEEE',
last_name: 'FFFF',
story: [....]
}
most_recent_story. 3. Sort bymost_recent_story.