I have following code:
.find(callback).limit(limit).sort({ id: -1 });
Which works like a charm, actually. However, few of the collections may have approved
field undefined
(it may not exist). If so, they should be moved at the end of the list.
Question: Can I just pass to that sort
function instead of that object { id: -1 }
?
My custom sorting function:
const o = [{
id: 4,
approved: true,
}, {
id: 1,
approved: true,
}, {
id: 2,
approved: true,
}, {
id: 3,
}];
const r = o.sort((a, b) => {
return b.approved ? b.id - a.id : b;
});
console.log(r);