Created
April 27, 2018 11:29
-
-
Save hiulit/5d6e86d1cb385c9e6ba8b9f16a257ce1 to your computer and use it in GitHub Desktop.
Simple function to sort an array of objects
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
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