Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active August 29, 2015 14:18
Show Gist options
  • Save kjbrum/8591343d83598dc53ef1 to your computer and use it in GitHub Desktop.
Save kjbrum/8591343d83598dc53ef1 to your computer and use it in GitHub Desktop.
Sort an array by a key value
/**
* Sort an array by a key value
*
* @param array array The array that needs to be sorted
* @param string key The value of the key to sort by
* @return array The sorted array
*/
function sortArrayByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key];
var y = b[key];
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