4

Possible Duplicate:
Sorting JavaScript Object by property value

Since I've noticed the browser can't guarantee the ordering of a JSON trough an ajax call (reference), I'm having some troubles with sorting the JSON element with javascript after the ajax call.

I've found some topics about this issue, but nothing gave me a clear solution for the simple structure of my JSON object. I just got 1 segment with a lot of data. I know there are a lot of topics about this, even on stackoverflow, but no topic handles this simple json structure.

For example:

{
   "158":"Banana",
   "265":"Pear",
   "358":"Apple",
   "864":"Peach"
}

How can I sort this object on the fruitnames instead of the id's? I'd like to have this object at the end:

{
   "358":"Apple",
   "158":"Banana",
   "864":"Peach"
   "265":"Pear",
}

Thanks in advance.

Community
  • 1
  • 1
Tim
  • 662
  • 1
  • 7
  • 16
  • 1
    See : http://stackoverflow.com/questions/8175093/simple-function-to-sort-a-json-object-using-javascript – pradeek Nov 23 '11 at 15:16
  • 1
    underscore.js has a nice `sortBy()` function: http://documentcloud.github.com/underscore/#sortBy – Teddy Nov 23 '11 at 15:18
  • 8
    Objects aren't in any particular order. If you want them in order, use an array (of objects) instead. – gen_Eric Nov 23 '11 at 15:18
  • 1
    That is not a JSON object. JSON is a string notation. You ***parse*** the JSON string ***into*** an ***object literal***. – Matt Nov 23 '11 at 15:49

2 Answers2

2
var kk = {
   "158":"Banana",
   "265":"Pear",
   "358":"Apple",
   "864":"Peach"
}

var keys = [];
var datas = {}

$.each(kk, function(key, value){

  keys.push(value)
  datas[value] = key;

})
    var aa = keys.sort()

        var sorted ={}

$.each(aa, function(index, value){

    sorted[datas[value]] = value;
})
erimerturk
  • 4,230
  • 25
  • 25
  • It's just the other way around. I want to sort on the names not on the id's, but I'm trying a solution with your code example. – Tim Nov 23 '11 at 15:52
  • The for each you use to create the sorted object, can be used to solve a problem. But keep this in mind **'Objects aren't in any particular order. If you want them in order, use an array (of objects) instead'**. Thank you for your time. – Tim Nov 29 '11 at 14:43
0
var someObject = {
   "158":"Banana",
   "265":"Pear",
   "358":"Apple",
   "864":"Peach"
};

//flip the name/value pairs
Object.prototype["reverseObject"] = function() {
    var name;
    var newObject = {};
    for(name in this) {
        if(typeof this[name] !== 'function') {
           newObject[this[name]] = name;
        }
    }
    return newObject;
}

var reversedObject = someObject.reverseObject();

console.log(reversedObject);

var i;
var properties = [];
//get all the property names
$.each(reversedObject, function(key, value) {
    if(reversedObject[key] !== this)
    properties.push(key);
});

properties.sort();

var sortedObject = [];

//iterate the array using the order specified
for(i = 0; i < properties.length; i += 1) {
    sortedObject[properties[i]] = reversedObject[properties[i]];
    //document.writeln(properties[i] + " : " + reversedObject[properties[i]]);
}

var sortedObject = sortedObject.reverseObject();

console.log(sortedObject);

Fiddle

This creates a sorted array of property names in alphabetical order. Then, this array is used to index the object. This results in the exact object you requested.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348