Skip to content

Instantly share code, notes, and snippets.

@jonatasnona
Created March 16, 2012 14:48
Show Gist options
  • Save jonatasnona/2050388 to your computer and use it in GitHub Desktop.
Save jonatasnona/2050388 to your computer and use it in GitHub Desktop.
Javascript: Sort an Array of Objects by Properties
// thanks to daniel walsh
// http://davidwalsh.name/array-sort
// http://dochub.io/#javascript and search for sort to see more details
// sort by age
var o = [{name : "Robin Van Persie", age: 28},
{name : "Theo Walcott", age : 22},
{name : "Bacary Sagna", age : 26}
].sort(function(obj1, obj2) {
return obj1.name - obj2.name;
});
// sort by name
var o = [{name : "Robin Van Persie", age: 28},
{name : "Theo Walcott", age : 22},
{name : "Bacary Sagna", age : 26}
].sort(function(obj1, obj2) {
return obj1.name > obj2.name;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment