2

I have an array of objects, for example:

var a = [
 { value: 500, name: 'ccc' },
 { value: 100, name: 'bbb' },
 { value: 500, name: 'aaa' },
 { value: 300, name: 'eee' },
];

And I need to sort it by descending order of value field, AND if value field are equals -- then sort this two objects by alphabet order of field name.

I try something like this:

a.sort(function (a, b) {
    return b["value"] - a["value"] || (a["name"] > b["name"]) ? 1: -1;
});

But this does not result in

500,aaa
500,ccc
300,eee
100,bbb

as I would expect

3
  • 2
    What means 'not working'? Any errors? Commented Jun 13, 2014 at 12:00
  • @mplungjan The order of your results is 300, 500, 100, 500. That's not descending order of field value. Commented Jun 13, 2014 at 12:04
  • That is not my result, I just pasted the code. Commented Jun 13, 2014 at 12:10

2 Answers 2

6

The problem is operator precedence, you need to parenthesize properly.

a.sort(function (a, b) {
    return (b["value"] - a["value"]) || ((a["name"] > b["name"]) ? 1: -1);
});

The logical operators have higher precedence than the ternary operator, so you need wrap the ternary expression in parentheses. I've added additional redundant parentheses to make everything explicit.

Tested result:

[{"value":500,"name":"aaa"},
 {"value":500,"name":"ccc"},
 {"value":300,"name":"eee"},
 {"value":100,"name":"bbb"}]

DEMO

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

1 Comment

You're absolutely right. It's about operator precedence. Your suggested solution should solve the problem.
-2

Try to create a separate function.

 function compare(a,b) {
  if (a.value< b.value)
     return -1;
  if (a.value> b.value)
    return 1;
  if(a.value == b.value)
    if (a.name< b.name)
     return -1;
  if (a.name> b.name)
    return 1;

  return 0;
}

a.sort(compare);

Here is working example : http://jsfiddle.net/8ZUdF/

1 Comment

Making it a separate function has only one impact : polluting the external name space.

Your Answer

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