Created
February 29, 2012 07:52
-
-
Save growdigital/1938970 to your computer and use it in GitHub Desktop.
Sort an Array of Objects by Property Using sort(fn)
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
[ | |
{ name: "Robin Van Persie", age: 28 }, | |
{ name: "Theo Walcott", age: 22 }, | |
{ name: "Bacary Sagna", age: 26 } | |
].sort(function(obj1, obj2) { | |
// Ascending: first age greater than the previous | |
return obj1.age - obj2.age; | |
}); | |
// Returns: | |
// [ | |
// { name: "Theo Walcott", age: 22 }, | |
// { name: "Bacary Sagna", age: 26 }, | |
// { name: "Robin Van Persie", age: 28 } | |
// ] | |
// via http://davidwalsh.name/array-sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment