Skip to content

Instantly share code, notes, and snippets.

@unknownuser88
Created January 6, 2014 15:56
Show Gist options
  • Select an option

  • Save unknownuser88/8284872 to your computer and use it in GitHub Desktop.

Select an option

Save unknownuser88/8284872 to your computer and use it in GitHub Desktop.
sort json by
var people = [
{'name': 'a75','item1': false,'item2': false},
{'name': 'z32','item1': true,'item2': false},
{'name': 'e77','item1': false,'item2': false},
];
function sortByKey(array, key, asc) {
var asc = asc || false;
return array.sort(function(a, b) {
var x = a[key],y = b[key];
return (!asc)?((x < y) ? -1 : ((x > y) ? 1 : 0)):((x > y) ? -1 : ((x < y) ? 1 : 0));
});
}
people = sortByKey(people, 'name');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment