While using the sort on an array I found that it produces an order of:
01….89AB….YZab….yz
I also found that I could sort numbers via their numeric value rather than their alphanumeric order,
(ie; 1 2 3 4 5 10 15 20 25 30 35 40 45 50
not 1 10 15 2 20 25 3 30 35 4 40 45 5 50)
with a slight modification to the sort function call.
Could someone tell me if I have overlooked some condition with this sort routine that follows?
My attempt was to sort the array elements similar to the normal sort
except that upper and lower case conditions were ignored
and the inclusion of a numeric sort order was included.
[code=php]
function AlphaNumSort(m,n) {
a = m.toLowerCase();
b = n.toLowerCase();
if (a == b) { return 0; } // values are equal
if (isNaN(m) || isNaN(n)) { // one or both are not numbers
return (a>b?1:-1);
} else {
return m-n; // both are numbers
}
}
……
// Used to sort randomized array like this:
NumStrArray.sort(AlphaNumSort);
It seems to work with all conditions I can come up with, ?
but I don’t know if I’ve thought it through enough.
Any comments?