Created
March 16, 2012 14:48
-
-
Save jonatasnona/2050388 to your computer and use it in GitHub Desktop.
Javascript: Sort an Array of Objects by Properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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