[Javascript] Sorting Problem
Hakan M (Backbase)
hakan at backbase.com
Tue Sep 14 10:56:58 CDT 2004
By the way,
element1 - element2;
isn't really doing much, since the result is not assigned anywhere. ;)
Regards,
H
Hakan M (Backbase) wrote:
> I assume that parseFloat works the same way as parseInt. It will return
> NaN if it cannot find a number, it will NOT return '0' since it doesn't
> convert to a number, it only tries to parse for a number.
>
> I think the following code will eliminate your error (not tested), but
> I'm not sure it will do what you want.
>
> function numeric_sort(element1, element2) {
> element1 = parseFloat(element1);
> element2 = parseFloat(element2);
> if(typeof(element1)!='number') element1 = 0;
> if(typeof(element2)!='number') element2 = 0;
> element1 - element2;
> return element1 - element2;
> }
>
> Regards,
> H
>
>
> Tim Makins wrote:
>
>> 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;
>> }
>>
>> _______________________________________________
>> Javascript mailing list
>> Javascript at LaTech.edu
>> https://lists.LaTech.edu/mailman/listinfo/javascript
>>
>>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
More information about the Javascript
mailing list