Skip to content

Instantly share code, notes, and snippets.

@hiulit
Created April 27, 2018 11:29
Show Gist options
  • Save hiulit/5d6e86d1cb385c9e6ba8b9f16a257ce1 to your computer and use it in GitHub Desktop.
Save hiulit/5d6e86d1cb385c9e6ba8b9f16a257ce1 to your computer and use it in GitHub Desktop.
Simple function to sort an array of objects
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key];
var y = b[key];
if (typeof x == "string") {
x = (""+x).toLowerCase();
}
if (typeof y == "string") {
y = (""+y).toLowerCase();
}
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment