Skip to content

Instantly share code, notes, and snippets.

@enricosoft
Created March 29, 2017 19:53
Show Gist options
  • Save enricosoft/3b6abef21b564337cafda7324bbfcb11 to your computer and use it in GitHub Desktop.
Save enricosoft/3b6abef21b564337cafda7324bbfcb11 to your computer and use it in GitHub Desktop.
Order people by age
var data = [
{
"name": "Enrico",
"age": 29
},
{
"name": "Mark",
"age": 32
},
{
"name": "Sarah",
"age": 27
},
{
"name": "Elon",
"age": 45
},
{
"name": "Robert",
"age": 34
}
];
var result = data.sort(function(a, b){
if (a.age < b.age) {
return -1;
}
if (a.age > b.age) {
return 1;
}
return 0;
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment