I am sorting an array of object using the date values inside each object.
I now have an edge case, if thenoDate
property is set to true, that object shall be sorted after the object with the id 2.
An object looks something like this:
{
id: 1 // just simple ids 1...99
date: ...
noDate: true // true or false
}
Currently its sorted like this, but that does not provide the logic I need.
data.sort((a, b) => (!a.noDate - !b.noDate) || new Date(a.date) - new Date(b.date))
CodePudding user response:
You could sort first to get all noDate: true
at top of the array and then map a new array to get all top items after id: 2
object. This approach needs a finally
let
data = [
{ id: 0, noDate: true },
{ id: 1, date: new Date('2021-10-30') },
{ id: 2, date: new Date('2020-10-30') },
{ id: 3, noDate: true },
{ id: 4, date: new Date('2021-09-30') },
{ id: 5, date: new Date('2020-09-15') },
{ id: 6, noDate: true }
];
data = data
.sort((a, b) => !a.noDate - !b.noDate || a.date - b.date)
.flatMap((a => o => {
if (o.noDate) { a.push(o); return []; }
if (o.id === 2) return [o, a];
return o;
})([]))
.flat();
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
simply
list.sort((a, b) => a.noDate ? -1 : new Date(a.date) - new Date(b.date))
Or if you want to sort by two field first sortNumber and then sorting by date
list.sort((a, b) => a.sortNumber == b.sortNumber ? new Date(a.date) - new Date(b.date): a.sortNumber - b.sortNumber)