Sort a JSON array object using Javascript by value [duplicate] Sort a JSON array object using Javascript by value [duplicate] json json

Sort a JSON array object using Javascript by value [duplicate]


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;    });


That's not JSON, and it's not an array. It's a regular JavaScript object, and you cannot impose an ordering on the properties of an object.

If you want to maintain the order of your elements, you need an array (again, this isn't JSON, it is JavaScript):

[ [ 'familia', '4633452'] ,  [ 'workhardplayhard', '761277'],  [ 'caffeineoverdose', '2517']]