[Javascript] Sorting Problem

Tim Makins spindrift at oceanfree.net
Tue Sep 14 10:07:47 CDT 2004


I have a multi-dimensional array; some columns contains strings of text, and
other columns contain numbers. The numbers in the array are stored as
strings.

When trying to sort the array, I tried the following functions with the
my_array.sort() command to get the different types of sort: dictionary
order, and numerical order. Both work fine on the columns containing
numbers, but if I try the numeric sort on a column containing strings, it
crashes with error 'Number Expected'. Is there an easy way out of this?

Tim in Ireland.

function alpha_sort(element1, element2) {
 if (element1 < element2) {return -1}
 else if (element1 > element2) {return 1}
 else {return 0}
}

function numeric_sort(element1, element2) {
 element1 = parseFloat(element1);
 element2 = parseFloat(element2);
  element1 - element2;
 return element1 - element2;
}




More information about the Javascript mailing list