Skip to content

Instantly share code, notes, and snippets.

@JoeChapman
Created March 22, 2012 13:45
Show Gist options
  • Save JoeChapman/2158435 to your computer and use it in GitHub Desktop.
Save JoeChapman/2158435 to your computer and use it in GitHub Desktop.
Sorting alphanumeric values in ascending order with a compare function
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]
@raycris
Copy link

raycris commented Oct 12, 2021

This method doesn't work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment