Created
March 29, 2017 19:53
-
-
Save enricosoft/3b6abef21b564337cafda7324bbfcb11 to your computer and use it in GitHub Desktop.
Order people by age
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
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