Created
September 9, 2017 22:47
-
-
Save brettbartylla/ef1183460ecf878b45fb7842ddc87537 to your computer and use it in GitHub Desktop.
Sorts a 2 dimensional array
This file contains 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
var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']]; | |
a.sort(sortFunction); | |
//display result | |
alert(a); | |
//sorts array by first array column, change values of 0 to 1 to sort by second column | |
function sortFunction(a, b) { | |
if (a[0] === b[0]) { | |
return 0; | |
} | |
else { | |
return (a[0] < b[0]) ? -1 : 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment