views:

1968

answers:

3

I've been looking for a while and want a way to sort a JSON object like this:

{
    method: 'artist.getInfo',
    artist: 'Green Day',
    format: 'json',
    api_key: 'fa3af76b9396d0091c9c41ebe3c63716'
}

and sort is alphabetically by name to get:

{
    api_key: 'fa3af76b9396d0091c9c41ebe3c63716',
    artist: 'Green Day',
    format: 'json',
    method: 'artist.getInfo'
}

I can't find any code that will do this. Can anyone give me some help?

A: 

http://www.javascriptkit.com/javatutors/arraysort.shtml - Decent tutorial on sorting complex JS objects.

http://www.highdots.com/forums/javascript/re-sorting-json-data-270187.html - Another example


Here is a full reference on the sort function if you want the details

https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/Array/sort

-Last edit, I swear!

apocalypse9
The problem I've got is that those articles show how to sort an array of objects. I'm trying to sort an object filled with objects. Will the same examples work for my case, if so could you provide an example?Sorry I'm just really stuck on this one :s
matto1990
+5  A: 

By definition, the order of keys in an object is undefined, so you probably won't be able to do that in a way that is future-proof. Instead, you should think about sorting these keys when the object is actually being displayed to the user. Whatever sort order it uses internally doesn't really matter anyway.

By convention, most browsers will retain the order of keys in an object in the order that they were added. So, you could do this, but don't expect it to always work:

function sortObject(o) {
    var sorted = {},
    key, a = [];

    for (key in o) {
     if (o.hasOwnProperty(key)) {
      a.push(key);
     }
    }

    a.sort();

    for (key = 0; key < a.length; key++) {
     sorted[a[key]] = o[a[key]];
    }
    return sorted;
}

Does that help?

Chris Nielsen
That seems to work perfectly. I'll have to keep an eye on it breaking in future browsers but it seems to work for now.Thanks for the help :)
matto1990
It may in fact always work, and it is being codified into the Ecmascript standard, but it's still not a safe assumption to rely on, because having a defined order is not logically part of an "object". If you want to define order, you should use an array.
Breton
The only reason I need to do it is to sort the values once so I can generate an api signature as defined on this page in section 6: http://www.last.fm/api/webauthIf I was using it for more than that I'd use an array and them sort that.
matto1990
You should most definately use an array for that.
Breton
A: 

this function takes an object and returns a sorted array of arrays of the form [key,value]

function (o) {
   var a = [],i, tmp;
   for(i in o){ 
     if(o.hasOwnProperty(i)){
         a.push([i,o[i]);
     }
   }
   a.sort(function(a,b){ return a[0]>b[0]?1:-1; })
   return a;
}

The object data structure does not have a well defined order. In mathematical terms, the collection of keys in an object are an Unordered Set, and should be treated as such. If you want to define order, you SHOULD use an array, because an array having an order is an assumption you can rely on. An object having some kind of order is something that is left to the whims of the implementation.

Breton