Created
September 8, 2018 21:02
-
-
Save ianfabs/0c6b912606b238b8d1739c736aa597b4 to your computer and use it in GitHub Desktop.
Sort a JSON Array by a key
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 GetSortOrder(prop) { | |
return function(a, b) { | |
if (a[prop] > b[prop]) { | |
return 1; | |
} else if (a[prop] < b[prop]) { | |
return -1; | |
} | |
return 0; | |
} | |
} | |
Array.prototype.sortBy = function(key){ | |
return this.sort( (a,b) => ( ( a[key] < b[key] ) ? -1 : ( ( a[key] > b[key] ) ? 1 : 0 ) ) ) | |
} | |
//.sort( (a,b) => ( ( a[key] < b[key] ) ? -1 : ( ( a[key] > b[key] ) ? 1 : 0 ) ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment