5

How do i sort this JSON object by first_name?

{
    1  :{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"[email protected]"},
    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:""}
}
4
  • 1
    Objects properties aren't ordered, so you can't sort them. Commented Jul 31, 2012 at 0:24
  • @Musa: of course you can. Not directly, but you certainly can sort keynames and do something in order. Commented Jul 31, 2012 at 0:26
  • None of these helped you in any way? Commented Jul 31, 2012 at 0:57
  • 1
    See : JQuery: Sorting JSON by property Commented Feb 19, 2013 at 3:53

3 Answers 3

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:"[email protected]"}, 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.

Sign up to request clarification or add additional context in comments.

2 Comments

the script is sorting and displaying the users correctly. is there a way to sort them permanently? i mean over-write the previous json?
@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.
3

Write your JSON object like the following:

data = [{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"[email protected]"}, {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;
});

Comments

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

1 Comment

the sort algo looks overly complicated, String.prototype.localeCompare will do the job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.