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?

Reported as a duplicate at Grundy. javascript Aug 31 '18 at 1:41 pm

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 .

  • the sort method takes a function with which you can sort - ThisMan

1 answer 1

 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));