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

2
  • b.start_date() it's not a function. Commented Mar 4, 2017 at 8: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? Commented Mar 4, 2017 at 8:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.)
FYI: status:1 appear to be coming last in your code, OP has asked for them to come first
@haxxxton You can simply reverse the expression to make status 1 appear first: b.status - a.status
The objects with status 1 should come first, then 0 should come
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.