Skip to content

Instantly share code, notes, and snippets.

@kuldeepkeshwar
Created December 20, 2016 21:09
Show Gist options
  • Save kuldeepkeshwar/95fc4ba37567b1b96fb83273f32c3417 to your computer and use it in GitHub Desktop.
Save kuldeepkeshwar/95fc4ba37567b1b96fb83273f32c3417 to your computer and use it in GitHub Desktop.
You have an array of objects in JavaScript. Each one contains a name (a string) and age (a number). Write a function which returns the objects ordered by age
function sortByAge(arr){
arr.sort(function(obj1,obj2){
if (obj1.age>obj2.age){
return -1;
}else{
return 1;
}else{
return 0;
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment