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.

4
  • 1
    See : stackoverflow.com/questions/8175093/… Commented Nov 23, 2011 at 15:16
  • 1
    underscore.js has a nice sortBy() function: documentcloud.github.com/underscore/#sortBy Commented Nov 23, 2011 at 15:18
  • 8
    Objects aren't in any particular order. If you want them in order, use an array (of objects) instead. Commented Nov 23, 2011 at 15:18
  • 1
    That is not a JSON object. JSON is a string notation. You parse the JSON string into an object literal. Commented Nov 23, 2011 at 15:49

2 Answers 2

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;
})
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.