Last active
November 28, 2016 08:40
-
-
Save siddharthpolisiti/37d5a1f62530f309b99351624aae0875 to your computer and use it in GitHub Desktop.
The program is to sort objects based upon the array object with key 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 arr=[{name:"sidd",age:26},{name:"john",age:30},{name:"gary",age:20}]; | |
function compare(a,b) { | |
if (a.age < b.age) | |
return -1; | |
if (a.age > b.age) | |
return 1; | |
return 0; | |
} | |
arr.sort(compare); | |
alert(JSON.stringify(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment