I have the following data structure:
var dates = [
{
id: '1',
date1: '2022-03-21T18:59:36.641Z',
date2: '2022-03-17T18:59:36.641Z',
},
{
id: '2',
date1: '2022-03-20T18:59:36.641Z',
date2: '2022-03-17T18:59:36.641Z',
},
{
id: '3',
date2: '2022-03-17T18:59:36.641Z',
},
{
id: '4',
date2: '2022-03-15T18:59:36.641Z',
}
];
var sorted = dates.sort(function(a,b) {
return (a.date1 > b.date1) ? 1 : -1
});
console.log({sorted});
Notice that date1 is not always available, but date2 is required. I'd like to sort by date1 first, then date2. I've created this fiddle to test, but still haven't figured it out: https://jsfiddle.net/y9sgpob8/4/
Please help me figure out how to get the results in the following order:
[{
date1: "2022-03-20T18:59:36.641Z",
date2: "2022-03-17T18:59:36.641Z",
id: "2"
}, {
date1: "2022-03-21T18:59:36.641Z",
date2: "2022-03-17T18:59:36.641Z",
id: "1"
}, {
date2: "2022-03-15T18:59:36.641Z",
id: "4"
}, {
date2: "2022-03-17T18:59:36.641Z",
id: "3"
}]