1

I have an object array

var arr= [  {vara:4, varb:0, varc:3}    {vara:1, varb:2, varc:0}    {vara:3, varb:1, varc:5} ..... ]

At first i want to sort the array by vara so i use

arr.sort(function(a, b){return a.vara-b.vara});

If the sorted array have equal values in vara parameter, i want to do a second sorting by varb that will only resort the objects with equal vara values and not all the array.

How is this possible?

2
  • Are your values always integers? Commented Nov 12, 2014 at 11:46
  • in this case they are.. Commented Nov 12, 2014 at 11:57

1 Answer 1

2

How about something like:

arr.sort(function(a, b){
    if (a.vara === b.vara) {
        return a.varb - b.varb
    }
    return a.vara-b.vara;
});
Sign up to request clarification or add additional context in comments.

Comments

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.