Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Created February 16, 2014 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 23maverick23/9036931 to your computer and use it in GitHub Desktop.
Save 23maverick23/9036931 to your computer and use it in GitHub Desktop.
JavaScript: Array sort using object key:values
[
{ name: "Robin", age: 30 },
{ name: "Theo", age: 24 },
{ name: "Bacary", age: 28 }
].sort(function(obj1, obj2) {
// Ascending: first age less than the previous
return obj1.age - obj2.age;
});
// Returns:
// [
// { name: "Theo", age: 24 },
// { name: "Bacary", age: 28 },
// { name: "Robin", age: 30 }
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment