Created
March 22, 2012 13:45
-
-
Save JoeChapman/2158435 to your computer and use it in GitHub Desktop.
Sorting alphanumeric values in ascending order with a compare function
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 arr = ['z', 564, 12, 785, 'a', 'dc', 'o', 43, 's', 'r']; | |
arr.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; | |
}); | |
// arr = [12,43,564,785,a,dc,o,r,s,z] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This method doesn't work for me