I have a JSON array and I am trying to sort it by value. The problem I am having is that I am not able to keep the JSON structure with my sorting.
Here is the JSON array:
{
  caffeineoverdose: '2517',
  workhardplayhard: '761277',
  familia: '4633452'
}
I would like something like this:
{
  familia: '4633452',
  workhardplayhard: '761277',
  caffeineoverdose: '2517
}
                Here is everything you need.
Like i said already in the comments you can't sort an object.. but you can put it into an array and display the results.
var array=[],obj={
 caffeineoverdose:'2517',
 workhardplayhard:'761277',
 familia:'4633452'
};
for(a in obj){
 array.push([a,obj[a]])
}
array.sort(function(a,b){return a[1] - b[1]});
array.reverse();
DEMO
http://jsfiddle.net/GB23m/1/
You could convert it into an array of objects:
[{ name: 'caffeineoverdose', number: '2517' }, {name: 'workhardplayhard', number: '761277'}, {name: 'familia', number: '4633452'}]
and then sort by number
array.sort(function(a,b){
    return a.number - b.number;
    }
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With