Skip to content

Instantly share code, notes, and snippets.

@adamculpepper
Created February 26, 2018 21:15
Show Gist options
  • Save adamculpepper/355db0a4d5ee5e6ed880b6d3adff0203 to your computer and use it in GitHub Desktop.
Save adamculpepper/355db0a4d5ee5e6ed880b6d3adff0203 to your computer and use it in GitHub Desktop.
JavaScript: Alphanumeric Array Sort
var array = ['A1', 'B12', 'C3', 11, 10, 1111, 22, 1, 4, 3, 5, 2, 0.23, 0];
function sortArray(input) {
var output = input.sort(function(a, b) {
if (a === b) {
return 0;
}
if (typeof a === typeof b) {
return a < b ? -1 : 1;
}
return typeof a < typeof b ? -1 : 1;
});
return output;
}
$("#output").text(sortArray(array)); // 0,0.23,1,2,3,4,5,10,11,22,1111,A1,B12,C3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment