I have an array with classes (the class is called Note), and in those classes are basic values. Two important values are the Note.Pinned (boolean whether it's pinned or not) and Note.Modified (a timestamp when it was last edited).
I would like to sort this array of classes by the Modified timestamp, then reorder it that the Pinned notes are on top of this list.
I already tried sorting the notes.
const SortedNotes = Notes.sort((a, b) => a.Modified > b.Modified);
Notes being the array with classes.
But, then, is there a better way to rearrange this array? Or is this the only method I can use?
SortedNotes.filter(n => n.Pinned).concat(SortedNotes.filter(n => !n.Pinned));
The above would work, and I know I can use Array.prototype.partition, so is there any other way of doing this?
Thanks
sort((a, b) => Number(b.Pinned) - Number(a.Pinned) || a.getTime() - b.getTime())In sorting terminology it's known as a compound index sort.(a, b) => a.Modified > b.Modifiedis an inherently unstable return value for sorting.Number(b.Pinned). I will try out how that all works and thanks for the answer!