Skip to content

Instantly share code, notes, and snippets.

@brettbartylla
Created September 9, 2017 22:47
Show Gist options
  • Save brettbartylla/ef1183460ecf878b45fb7842ddc87537 to your computer and use it in GitHub Desktop.
Save brettbartylla/ef1183460ecf878b45fb7842ddc87537 to your computer and use it in GitHub Desktop.
Sorts a 2 dimensional array
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