-1

I need to sort keys in an object by using its values. I tried to use javascript's sort method but could not figure it out yet. My object looks like:

var myJson = [
        {
        "4": 3,
        "5": 3,
        "14": 3,
        "18": 3,
        "20": 1,
        "23": 3,
        "25": 3,
        "33": 3,
        "36": 3
    }
];

I need to sort name, key pair in way so its ordered to:

var myJson = [
        {
        "4": 3,
        "5": 3,
        "14": 3,
        "18": 3,
        "23": 3,
        "25": 3,
        "33": 3,
        "36": 3,
        "20": 1
    }
];
Joe
  • 46,419
  • 33
  • 155
  • 245
Imran
  • 1,094
  • 1
  • 21
  • 41
  • 11
    Objects do not have order! – epascarello Nov 26 '13 at 16:25
  • What are you trying to accomplish? An object doesn't work the same as an array. "Sorting" this object wouldn't do anything – Señor Reginold Francis Nov 26 '13 at 16:29
  • Edited question makes no difference, Objects still do not have order. http://stackoverflow.com/q/5525795/14104 – epascarello Nov 26 '13 at 16:37
  • About your edit: you can of course sort keys in a JSON string (though your example does not contain JSON at all) but once you decode it there's no guarantee that the order will be respected, unless you write your own JSON parser. – Álvaro González Nov 26 '13 at 16:37
  • What you want is an array where each index points to an object. Not an array with one index that contains one object. Then you can use `sort` (with a custom sorting function). Ultimately, it comes down to the fact that you're using the wrong data structure for your needs. – Adam Jenkins Nov 26 '13 at 16:57
  • 1
    This is nothing to do with JSON. – Joe Nov 26 '13 at 17:11

3 Answers3

2

Assuming you want to retrieve the elements from the object in sorted order. You could get the keys from the object and sort the keys and retrieve the values from the object. See below,

var keys = [];
for(var k in obj) keys.push(k);
keys.sort();

for (var i = 0; i < keys.length; i++) {
  console.log(obj[key]);
}
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

As has been pointed out, objects don't have an order. So you would have to change your object structure to allow for sorting, something like this:

var myJson = [
    { "itemA" : { "value1": "4", "value2" : 3 } },
    { "itemB" : { "value1": "5", "value2" : 3 } },
    { "itemC" : { "value1": "14", "value2" : 3 } },
    { "itemD" : { "value1": "18", "value2" : 3 } }
];

Now you have an array of objects that can be sorted by any of the values through a custom sort function.

myJson.sort(function(a,b) {
    if (a.value2 == b.value2) {
        return parseInt(a.value1,10) - parseInt(b.value1,10);
    }
    return a.value2 - b.value2; 
});

Of course the above function only works if the values in value2 are integers and the values in value1 are string representations of integers... but you get the idea.

Steve
  • 8,609
  • 6
  • 40
  • 54
0

As mentioned before, you can't sort an object, but you can get those values out in the right order (in an array). The you could run through the array in order and either pull the keys or pull the values or whatever you need.

The following script takes the first object off the myJson array and builds a new array of arrays where each entry is [ key, value ]

// pull the hash out of the array
var j = myJson[0]; // first element is the hash

// for each hash entry, build an array pair
var as_array = []
for(k in j) { 
   as_array.push([parseInt(k,10), j[k]]); 
}
// sort first by value desc, then by key asc
var sorter = function(a,b) { return (a[1] < b[1]) && (b[0] > a[0]); }
var sorted = as_array.sort(sorter)

// show the results
sorted.forEach(function(el) { console.log(el); }

I also noticed that the sorting seems more complicated than just .sort because you want to sort by values first (descending) then by keys - I hope I read that correctly.

mr rogers
  • 3,200
  • 1
  • 19
  • 31