success_ny@yahoo.com wrote:
> Does anyone have a code snippet to compare those values so I can sort
> the array of alpha-numeric values that include both characters and
> integers in it?
>
> I.e., if we have values like 4236 and 123234, I want 4236 to be second
> because 4 is bigger than 1 rather than using the numeric comparison.
> The strings can include character values and strings. Basically, I have
> the bubble sort function, the question is how to compare those types of
> strings in the alpha-numeric order.
>
> i.e.,
>
> A83745
> B34974
> 127734
> 34456
> 788
>
> I looked all over the web thinking that this simple question would be
The time I wrote this script (a while ago) I was crying for Perl with
his "cmp" and shuttle <=> Get's really "if'y" in JavaScript w/o them
:-)
function alfanum(a,b) {
var result = 0;
if ((typeof(a)=='number')&&(typeof(b)=='number')) {
result = a-b;
}
else if ((typeof(a)=='string')&&(typeof(b)=='string')) {
if (a < b) {result = -1;}
else if (a > b) {result = 1;}
else {result = 0;}
}
else if (typeof(a)=='string'){
result = 1;
}
else {
result = -1
}
return result;
}
var arr = [9,'A',8,'B',7,'C',6,'D',5,'E',4,'F',3,'G',2,'H',1];
alert(arr.sort(alfanum));
Received on Tue Oct 18 02:59:19 2005