Sorting - Reg.

Hi masters,
I have one doubt,
lets's say file1 has the following contents,

1
2.0
3.1
5.5
7
5.10
5.9

How to sort these contents to get the o/p like

1
2.0
3.1
5.5
5.9
5.10
7

Note:5.10 should come after 5.9.
Please provide your comments.
Thank you

sort -t '.' -k1,1 -k2,2 -n myFile

I get an error with the code of vgersh99, try:

sort -t '.' -k 1,1n -k 2,2n file

strange - works fine under Solaris9.

you can try - not sure the order matters:

sort -t '.' -n -k1,1 -k2,2 myFile

A quick observation - neither of these solutions work for non-trivial floating point numbers, because the order of the most significant digit is the reverse on the right of the decimal point as compared to the left.

So, while 1034 < 10034 is true; 0.1034 < 0.10034 isn't.

An example is below:

$
$
$ cat f0
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$
$ sort -t"." -k1,1n -k2,2n f0
1
2.0
2.01
3.1
5.5
5.9
5.10
5.1034
5.10034
7
$
$

Note that the floating point numbers in red are not in ascending order.

I am sure will be something for this in the shell itself, but here's a simple Perl solution:

$
$ cat f0
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$
$ perl -lne 'chomp; push @x, $_; END {print foreach sort @x}' f0
1
2.0
2.01
3.1
5.10
5.10034
5.1034
5.5
5.9
7
$
$

tyler_durden

$  cat file
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$ sort -kn1 file
1
2.0
2.01
3.1
5.10
5.10034
5.1034
5.5
5.9
7

I got the error with a HP-UX on my work but it works fine on my Debian system.

anbu23 and durden_tyler, the second column must be also sorted in numeric order so the output should be like:

3.1
5.5
5.9
5.10
5.1034
5.10034
7

I didnt read the requirements properly.

That's right; my bad... gotta read those requirements carefully.

tyler_durden