11

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.

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
Scott Chantry
  • 1,115
  • 5
  • 16
  • 29
  • 1
    Your example is for a hash of object properties. By definition, these are not sorted. I assume you have an array of these objects that you want to sort by? – Jason McCreary Jul 12 '10 at 16:05
  • Is it an array of JSON objects? – Castrohenge Jul 12 '10 at 16:10
  • Does this answer your question? [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – Vega Mar 07 '20 at 08:02

2 Answers2

42

Here is a generic way to sort an array of objects, with multiple columns:

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

// generic comparison function
cmp = function(x, y){
    return x > y ? 1 : x < y ? -1 : 0; 
};

//sort name ascending then id descending
arr.sort(function(a, b){
    //note the minus before -cmp, for descending order
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id)], 
        [cmp(b.name, a.name), -cmp(b.id, a.id)]
    );
});

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

arr.sort(function(a, b){
    return cmp( 
        [cmp(a.name, b.name), -cmp(a.id, b.id), cmp(a.other, b.other), ...], 
        [cmp(b.name, a.name), -cmp(b.id, a.id), cmp(b.other, a.other), ...]
    );
});

EDIT: per @PhilipZ comment below, the array comparison in JS convert them in strings separated by comas.

Mic
  • 24,812
  • 9
  • 57
  • 70
  • 1
    One for this answer is enough ;) Glad to see it helped you! – Mic May 11 '11 at 09:41
  • Question Why the arrays? Why not just `(a.name - b.name) || (a.id - b.id)` And you can go on like: `(a.name - b.name) || (a.id - b.id) || (a.idd - b.idd)` – Jimmy Kane Jun 21 '13 at 15:57
  • @JimmyKane `a.name - b.name` is a difference of strings and will return NaN. Why do you think it would work? – Mic Jun 23 '13 at 19:29
  • Correct. I was using if for distances. So the array comparison is for the strings. Ouf and I was a bit confused. – Jimmy Kane Jun 24 '13 at 07:54
  • The array is not only for strings but generic for different types. You can rely on it for any sort. I'm not sure to understand why a sequence of ... || ... would sort numbers correctly. – Mic Jun 25 '13 at 08:33
  • This is wrong: [2] < [10] returns "false" in JS, because all the arrays are converted to strings (with "," as the joining string) and then sorted lexicographically. – Philip Z Aug 29 '13 at 15:43
  • @PhilipZ, Very interesting. Quite luckily cmp returns only 1, -1 or 0 so it works in the two cases(arrays and values). I removed the wrong example. Thanks! – Mic Aug 29 '13 at 20:15
12

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.

Community
  • 1
  • 1
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 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 Jul 12 '10 at 17:44
  • @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 Jul 12 '10 at 18:02