Forum Moderators: open
Now of course the normal javascript sort function is putting these in order 1, 10, 2, 5. So i saw the bubblesort, with the same results, then tried the
sort(compareNumbers)
function compareNumbers(a,b){return a - b}
which just doesnt work
then tried
function compare(a, b) {
if (a < b)
return -1
if (a > b)
return 1
}
which works the exact same way as just plain sort.
Now im assuming im having problems with compareNumbers because their is more than just a number for each array value. Is there a way to do this that will see the number at the beginning of the array value and sort it numerically?
My current script looks like this:
function compare(a, b) {
if (a < b)
return -1
if (a > b)
return 1
}
function array_builder(){
ec_array = "";
ecstring = document.add.episodecomments.value;
ec_array = ecstring.split("-");
count = 0;
ecstring = "";
ec_array.sort(compare);
while (count < ec_array.length){
if(count > 0){
ecstring+= "-" + ec_array[count];
} else {
ecstring+= ec_array[count];
}
count+=1;
}
alert ec_array;
document.add.episodecomments.value = ecstring;
}
Thanx!
function compare(a, b) {
return intval(a) < intval(b);
} ;)