I have an Array of Object. I need to sort it using two conditions.
[{
id: 412,
start_date: 1488479400,
status: 1
}, {
id: 560,
start_date: 1499451100,
status: 0
}, {
id: 112,
start_date: 1499091200,
status: 0
}, {
id: 512,
start_date: 1488474500,
status: 1
}, {
id: 750,
start_date: 1483473100,
status: 1
}, {
id: 123,
start_date: 1499106600,
status: 0
}, ]
I need to sort this using two conditions.
- All the object with status 1 should come first
- The Date should be in descending order i.e., Highest date first.
Here's the expected output
[{
id: 750,
start_date: 1483473100,
status: 1
}, {
id: 512,
start_date: 1488474500,
status: 1
}, {
id: 412,
start_date: 1488479400,
status: 1
}, {
id: 112,
start_date: 1499091200,
status: 0
}, {
id: 123,
start_date: 1499106600,
status: 0
}, {
id: 560,
start_date: 1499451100,
status: 0
}, ]
What i tried is followed this answer
Assigned the array to data and then
data.sort(function(a,b){return a.start_date - b.start_date()});
But it didn't sort using the start_date