5

How do i sort this JSON object by first_name?

{
    1  :{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"user1@test.com"},
    7  :{user_id:7, first_name:"user2", last_name:"test2", email:""}, 
    72 :{user_id:72, first_name:"testing343", last_name:"", email:""},
    246:{user_id:246, first_name:"aaa", last_name:"ssss", email:""}
}
Hugolpz
  • 17,296
  • 26
  • 100
  • 187
Shounak Gupte
  • 69
  • 1
  • 1
  • 6
  • 1
    Objects properties aren't ordered, so you can't sort them. – Musa Jul 31 '12 at 00:24
  • @Musa: of course you can. Not directly, but you certainly can sort keynames and do something in order. – jAndy Jul 31 '12 at 00:26
  • None [of these](http://stackoverflow.com/search?q=jquery+sort+json) helped you in any way? – Felix Kling Jul 31 '12 at 00:57
  • 1
    See : [JQuery: Sorting JSON by property](http://stackoverflow.com/questions/881510/jquery-sorting-json-by-properties/14949429#14949429) – Hugolpz Feb 19 '13 at 03:53

3 Answers3

4

First of all, your JSON string is invalid. Clear that up first, you are forced to eval() the string in its current form, JSON.parse() won't work.


Object keys are unordered by spec. You can't statically "sort" key names and freeze them in an object. But you certainly can sort the keynames and operate in that order on their values.

For instance:

var json = '{1:{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"user1@test.com"}, 7:{user_id:7, first_name:"user2", last_name:"test2", email:""}, 72:{user_id:72, first_name:"testing343", last_name:"", email:""}, 246:{user_id:246, first_name:"aaa", last_name:"ssss", email:""}}';

json = eval( json );

Object.keys( json ).sort(function( a,b ) {
    return json[a].first_name.localeCompare( json[b].first_name );
}).forEach(function( key ) {
    console.log( json[key].first_name );
});

That code assumes the availabilty of an ES5 enabled JS engine or any kind of ES5-shim library.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • the script is sorting and displaying the users correctly. is there a way to sort them permanently? i mean over-write the previous json? – Shounak Gupte Jul 31 '12 at 00:56
  • @JSnewbie: no, not really. Of course you could create an ugly and clunky freezed `json string`, but that won't help you at all. The second you `eval` or `parse` it into an ECMAscript object, key names are random again. Your only choice is to sort it "live" and act on the key/values in order just then. – jAndy Jul 31 '12 at 00:58
3

Write your JSON object like the following:

data = [{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"user1@test.com"}, {user_id:7, first_name:"user2", last_name:"test2", email:""}, {user_id:72, first_name:"testing343", last_name:"", email:""}, {user_id:246, first_name:"aaa", last_name:"ssss", email:""}]

So that it's now an array. Then you can call .sort() on it.

data.sort(function(a, b) {
    var x = a.first_name.toLowerCase(), y = b.first_name.toLowerCase();
    return x < y ? -1 : x > y ? 1 : 0;
});
darksky
  • 1,955
  • 16
  • 28
0

Why do you want to sort an object?

You'll want to loop this object into an array, and then sort the array. http://jsfiddle.net/z2xsC/ For example:

//loop into array
var arr = [];
for( var key in data ){
  if( data.hasOwnProperty( key ) ){
    arr.push( data[key] );
  }
}

//now, sort your array
arr.sort( function(a,b) { 
  var nameA=a.first_name.toLowerCase(),
      nameB=b.first_name.toLowerCase();

  if (nameA < nameB){ 
    return -1; 
  } else if (nameA > nameB){ 
    return 1
  } else { 
    return 0
  }
});
Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
  • the sort algo looks overly complicated, `String.prototype.localeCompare` will do the job. – jAndy Jul 31 '12 at 00:54