How to sort floating point number

How to sort floating point number

Post by Zul Kher » Fri, 28 Mar 2003 04:54:08



How do I sort a file which has floating point numbers in asc/descending order ?

e.g.

2.1
2.10
1.1
1.2.
2.5

Thanks in advance

Zul

 
 
 

How to sort floating point number

Post by Stephane CHAZELA » Fri, 28 Mar 2003 05:51:25



> How do I sort a file which has floating point numbers in asc/descending order ?

> 2.1
> 2.10
> 1.1
> 1.2.
> 2.5

$ sort -t. -k1n,1n -k2 << EOF
heredoc> 2.1
heredoc> 2.10
heredoc> 1.1
heredoc> 1.2.
heredoc> 2.5
heredoc> EOF
1.1
1.2.
2.1
2.10
2.5

This won't handle correctly 1e+6, negative numbers...

--
Stphane

 
 
 

How to sort floating point number

Post by Absolute Newb » Fri, 28 Mar 2003 09:48:52


-----------------

Quote:> How do I sort a file which has floating point numbers in asc/descending order ?

> e.g.

> 2.1
> 2.10
> 1.1
> 1.2.
> 2.5

sort -n your_file
 
 
 

How to sort floating point number

Post by Barry Margoli » Fri, 28 Mar 2003 10:30:06





>-----------------
>> How do I sort a file which has floating point numbers in asc/descending order ?

>> e.g.

>> 2.1
>> 2.10
>> 1.1
>> 1.2.
>> 2.5

>sort -n your_file

That does integer sort, not floating point.  It will ignore everything
after the ".".

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to sort floating point number

Post by Absolute Newb » Fri, 28 Mar 2003 17:49:14


---------------------

Quote:> >sort -n your_file

> That does integer sort, not floating point.  It will ignore everything
> after the ".".

Well, with GNU sort it worked fine for me.
 
 
 

How to sort floating point number

Post by Barry Margoli » Sat, 29 Mar 2003 06:52:30





>---------------------
>> >sort -n your_file

>> That does integer sort, not floating point.  It will ignore everything
>> after the ".".

>Well, with GNU sort it worked fine for me.

Since this is comp.unix.shell, I think we should assume a generic Unix sort
command unless the OP specifies otherwise.  Or if a solution requires a
special version of a command, the poster should be specific.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to sort floating point number

Post by laura fairhe » Sat, 29 Mar 2003 08:31:05




Quote:

>$ sort -t. -k1n,1n -k2 << EOF
>heredoc> 2.1
>heredoc> 2.10
>heredoc> 1.1
>heredoc> 1.2.
>heredoc> 2.5
>heredoc> EOF
>1.1
>1.2.
>2.1
>2.10
>2.5

>This won't handle correctly 1e+6, negative numbers...

ie; floating point numbers. This is a FIXED-point sort

In all probability of course the OP used the term 'floating point'
when they meant 'fixed point' which seems to be a very common confusion
of terms.

byefornow
laura

--
alt     | Madonna's done something to try to stop this war,
fan     | Why don't you?  Mail this interesting article
madonna | to as many people as you can;
        | http://seattle.indymedia.org/front.php3?article_id=23919&group=webcast
        | LOVE & PEACE!

 
 
 

How to sort floating point number

Post by Barry Margoli » Sat, 29 Mar 2003 10:55:26






>>$ sort -t. -k1n,1n -k2 << EOF
>>heredoc> 2.1
>>heredoc> 2.10
>>heredoc> 1.1
>>heredoc> 1.2.
>>heredoc> 2.5
>>heredoc> EOF
>>1.1
>>1.2.
>>2.1
>>2.10
>>2.5

>>This won't handle correctly 1e+6, negative numbers...

>ie; floating point numbers. This is a FIXED-point sort

>In all probability of course the OP used the term 'floating point'
>when they meant 'fixed point' which seems to be a very common confusion
>of terms.

Actually, he probably meant "numbers with decimal points".  Fixed point and
floating point refer to ways that numbers are represented internally, not
how they're printed.

Fixed point also implies a specified maximum number of digits before and
after the decimal point.  E.g. in PL/I you might declare a variable to be
FIXED BIN(9,3), meaning 9 total bits with 3 bits of fraction, or FIXED
DEC(12,2) for 12 total digits with 2 after the decimal point.  Fixed point
is typically represented internally as a scaled integer.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to sort floating point number

Post by slimzh » Sat, 29 Mar 2003 15:35:48






> >---------------------
> >> >sort -n your_file

> >> That does integer sort, not floating point.  It will ignore everything
> >> after the ".".

  -g               compare according to general numerical value, imply -b

just using -g if available.

 
 
 

How to sort floating point number

Post by Peter J. Ackl » Mon, 31 Mar 2003 20:43:57



> How do I sort a file which has floating point numbers in
> asc/descending order ?

> e.g.

> 2.1
> 2.10
> 1.1
> 1.2.
> 2.5

    perl -e 'print sort { $a <=> $b } <>' FILE

Peter

--
I wish dialog boxes had a butten saying "Whatever".  I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever.  There ought to be a button matching my indifference.