From: darkon on
mdudley <mdudley(a)king-cart.com> wrote:

> Is there any way to sort floating point numbers in perl? Every
> example I have tried has failed. Numerical sort appears to NOT to
> be a numerical sort but rather an integer sort.

I suspect that reading the docs for sort() will help. There are even
examples.
From: John Bokma on
Steve C <smallpond(a)juno.com> writes:

> mdudley wrote:
>> Is there any way to sort floating point numbers in perl? Every
>> example I have tried has failed. Numerical sort appears to NOT to be
>> a numerical sort but rather an integer sort.
>>
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7

Coincidence. That's why you must read documentation...

perl -e 'print join("\n", sort ( 0.0001, 0.000002, 0.1 ))'
0.0001
0.1
2e-06


Note that sort defaults to string compare:

perl -e 'print join("\n", sort { $a <=> $b } ( 0.0001, 0.000002, 0.1 ))'
2e-06
0.0001
0.1

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: Tad McClellan on
Steve C <smallpond(a)juno.com> wrote:
> mdudley wrote:
>> Is there any way to sort floating point numbers in perl? Every
>> example I have tried has failed. Numerical sort appears to NOT to be
>> a numerical sort but rather an integer sort.
>>
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7


Doesn't work for me...

perl -e 'print join ("\n", sort (2.0, 12.0))'
12
2

Your's is not a numeric sort at all, it is a stringwise sort.

perl -e 'print join ("\n", sort {$a <=> $b} (2.0, 12.0))'
2
12


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.