Skip to content

Instantly share code, notes, and snippets.

@siddharthpolisiti
Last active November 28, 2016 08:40
Show Gist options
  • Save siddharthpolisiti/37d5a1f62530f309b99351624aae0875 to your computer and use it in GitHub Desktop.
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.
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