[Javascript] Sorting Problem

Tim Makins spindrift at oceanfree.net
Tue Sep 14 11:19:18 CDT 2004


No, that doesn't work - it still errors 'Number Expected'

Tim in Ireland.


----- Original Message ----- 
From: "Hakan M (Backbase)" <hakan at backbase.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, September 14, 2004 4:55 PM
Subject: Re: [Javascript] Sorting Problem


> 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