Last active
August 29, 2015 14:18
-
-
Save kjbrum/8591343d83598dc53ef1 to your computer and use it in GitHub Desktop.
Sort an array by a key value
This file contains hidden or 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
/** | |
* 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