views:

78

answers:

3

I have a JSON object that I want to sort by one key first, then by a second key similar to ordering by two columns in SQL. Here is a sample of the JSON I would have:

{ "GROUPID":3169675, "LASTNAME":"Chantry" }

I would like to order all the results by the GROUPID and then by LASTNAME. I've used the JSON sort function to sort by one key but not multiple. Any help would be great.

A: 

I think you will find the answer for sorting by two colums in the answers for this question: http://stackoverflow.com/questions/979256/how-to-sort-a-json-array

René Wolferink
I've read through that answer but did not find any information on how to sort by two columns, only one.
Scott Chantry
+5  A: 

Assuming you have an array of objects:

var data = [
    { "GROUPID":3169675, "LASTNAME":"Chantry" },
    { "GROUPID":3169612, "LASTNAME":"Doe" },
    ...
];

You can use a custom comparator to do the sorting. To sort first by GROUPID, and then by LASTNAME, the logic to compare two objects would be:

if GROUPID of first is smaller than second
    return -1;
else if GROUPID of first is larger than second
    return 1;
else if LASTNAME of first is smaller than second
    return -1;
else if LASTNAME of first is larger than second
    return 1;
else
    return 0;

To sort the object array, use the above algorithm and call the sort method on the array. After sorting is done, data should have the elements in required sorted order.

data.sort(function(a, b) {
    // compare a and b here using the above algorithm
});

Here's another very similar question I answered recently. It's regarding sorting on multiple columns using jQuery, but you can strip out the jQuery part easily. It presents some customizable approaches which can extend to multiple columns.

Anurag
Why don't you compare an array of keys? It make the sort more generic or do I miss something? See my response below.
Mic
@Mic - that is exactly what I am doing in the linked answer. I didn't want to rewrite the entire thing again, so just added a link to it.
Anurag
+1  A: 

Here is a generic way to sort an array of objects, with multiple columns:
The magic little secret here is comparing arrays.

    var arr = [
        { id:5, name:"Name3" },
        { id:4, name:"Name1" },
        { id:6, name:"Name2" },
        { id:3, name:"Name2" }
    ];

    //sort name ascending then id ascending
    arr.sort(function(a, b){
        //note the array comparison [...] < [...]
        return [a.name, a.id] < [b.name, b.id] ? -1 : 1;
    });

Now if you want to have an ascending or descending order we can do that:

    //sort name ascending then id descending
    function cmp(x, y){ // generic comparison function
        return x > y ? 1 : x < y ? -1 : 0; 
    }
    arr.sort(function(a, b){
        //note the minus before -cmp, for descending order
        return [cmp(a.name, b.name), -cmp(a.id, b.id)] 
             < [cmp(b.name, a.name), -cmp(b.id, a.id)] ? -1:1;
    });

To add other columns to sort on, you can add other items in the array comparison.

Mic