0

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.

  1. All the object with status 1 should come first
  2. 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

Here's my Fiddle

Community
  • 1
  • 1
SA__
  • 387
  • 2
  • 5
  • 12
  • `b.start_date()` it's not a function. – Fabian Klötzl Mar 04 '17 at 08:16
  • 1
    The format of the dates is not the same in your fiddle (indeed, in your fiddle it's probably not doing what you expect because `01-01-2017` *without* quotes is the number -2017 (1 minus 1 minus 2017). Does your real data have the `start_date` as shown in your question? – nnnnnn Mar 04 '17 at 08:16

1 Answers1

3

You could use Array#sort and a sort function with a chained approach for the sort criteria. You could use EPOCH time directly.

It wotks with evaluating the first delta and check if the vakue us truthy, that neasb the value is either smaller than one or grater than one in this case. If the value is zero, then both status values are equal and the next delta for the time is evaluated. Then the result is returned.

 delta       delta
status    start_date    comment
------    ----------    --------------------------
 < 0                    sort by status only to top
   0      evaluate      sort by start_date as well
 > 0                    sort by status only to bottom

var array = [{ 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 }];

array.sort(function (a, b) {
    return b.status - a.status || b.start_date - a.start_date;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • Nice use of `||` - I hadn't thought to do that in a sort function before. I think you've got the primary sort backwards though. (I suspect the OP will need a bit more explanation on how it works, too.) – nnnnnn Mar 04 '17 at 08:18
  • FYI: `status:1` appear to be coming last in your code, OP has asked for them to come first – haxxxton Mar 04 '17 at 08:23
  • @haxxxton You can simply reverse the expression to make status 1 appear first: b.status - a.status – ifadey Mar 04 '17 at 08:25
  • The objects with status `1` should come first, then `0` should come – SA__ Mar 04 '17 at 08:26
  • @ifadey, i agree that it is simple :) if it is to be marked as the answer, i would hope it conforms to the criteria outlined by OP – haxxxton Mar 04 '17 at 08:28