This question has already been answered:
We have an array of objects, each of which has a 'read' flag: {true | false}
How to implement array sorting so that objects with the 'read: true' flag fall at the end of the list?
This question has already been answered:
We have an array of objects, each of which has a 'read' flag: {true | false}
How to implement array sorting so that objects with the 'read: true' flag fall at the end of the list?
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
arr.sort((a, b) => { if (a.read == b.read) return 0; else if (a.read) return 1; else return -1; }); or
arr.sort((a, b) => (a.read? 1 : -1) - (b.read? 1 : -1)); var arr = [ { id: 1, read: true }, { id: 2, read: false }, { id: 3, read: true }, { id: 4, read: false }, { id: 5, read: true } ]; arr.sort((a, b) => a.read - b.read); console.log(JSON.stringify(arr)); Source: https://ru.stackoverflow.com/questions/875877/
All Articles
sortmethod takes a function with which you can sort - ThisMan