Created
February 26, 2018 21:15
-
-
Save adamculpepper/355db0a4d5ee5e6ed880b6d3adff0203 to your computer and use it in GitHub Desktop.
JavaScript: Alphanumeric Array Sort
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
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