Created
December 20, 2016 21:09
-
-
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
This file contains hidden or 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
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