1
/*
        I have two people
*/

var person_one= 'person_one';
var person_two= 'person_two';

/*
    This is a response I get from a server
*/

var response = [
{"key":{"name":"person_one","kind":"attrA","path":["attrA","person_one"]},"data":{"q":"3","n":"0"}},
{"key":{"name":"person_one","kind":"attrB","parent":{"name":"person_two","kind":"attrA","path":["attrA","person_two"]},"path":["attrA","person_two","attrB","person_one"]},"data":{"some_data":"p"}},
{"key":{"name":"person_two","kind":"attrA","path":["attrA","person_two"]},"data":{"q":"1","n":"0"}}
];

/*
    Sort the object array by the key.kind field. So all attrA will come first and then attraB
*/

function compare(a,b) {
  if (a.key.kind < b.key.kind )
    return -1;
  else if (a.key.kind  > b.key.kind)
    return 1;
  else 
    return 0;
}

response.sort(compare); //sort

console.log('Sorted response = ' + JSON.stringify(response) ); //print sorted response

This is the code I have so far. What is happening is I get a response from the server which is:

[
{"key":{"name":"person_one","kind":"attrA","path":["attrA","person_one"]},"data":{"q":"3","n":"0"}},
{"key":{"name":"person_one","kind":"attrB","parent":{"name":"person_two","kind":"attrA","path":["attrA","person_two"]},"path":["attrA","person_two","attrB","person_one"]},"data":{"some_data":"p"}},
{"key":{"name":"person_two","kind":"attrA","path":["attrA","person_two"]},"data":{"q":"1","n":"0"}}
]

I need to now sort this response using two following conditions:

  • The first condition is to sort it by the key.kind attribute which the code does. All attraA will need to show first, then attraB

  • The second condition is after this is sorted by key.kind it then needs to sort it by name. So that person_one will always come before person_two (these are just fake names to show the concept, but actual names could be like jack or jill, etc). So I am able to sort by key.kind, but I am unsure how to add a second sort condition now and sort it by it. If this was SQL it would be similar to something such as this: SELECT * FROM table ORDER BY KIND,NAME; -- sort by KIND first then NAME

1
  • You might want to remove unnecessary details from your question. What does the code you "have so far" to do with your question? Also, var person_one= 'person_one'; var person_one = 'person_two'; should probably be fixed. Commented May 22, 2016 at 1:36

1 Answer 1

2

Let's assume your server response is stored within array people. Then you can use the Array sort() method with a user defined compare function:

people = people.sort(function(a, b) {
    if (a.key.kind != b.key.kind) return a.key.kind > b.key.kind;
    else return a.key.name > b.key.name;
});
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.