I got a list of object, some of them as a date property completedAt
, but some of them don't. I want the most recent completed object first in the list, the ones that does not have a completedAt
property I want to appears last, and I do not care about their order, as long as the are after alle the dated objects.
const list = [
{
id: '40',
completedAt: '2019-08-06T09:50:39.219Z'
},
{
id: '45'
},
{
id: '73',
completedAt: '2018-08-06T09:51:19.362Z'
},
{
id: '74',
completedAt: '2019-08-06T09:51:00.279Z'
},
{
id: '96',
completedAt: '2019-08-06T09:51:12.761Z'
}
];
const sortedList = list.sort((a, b) => { return new Date (b.completedAt) - new Date (a.completedAt) });
console.log(sortedList)
This function does do something to the order of the list, but they are not sorted correctly.
FYI I also have Ramda.js available if you know any ramda trick, they are more than welcome?