How to sort a string with numbers

Hi,

I need help to sort a file contents.

I am using sort -r option to basically reverse the comparison in descending order. However, i found out that my file is not sorted according, can anyone please help.

My data is something like:-
Hello world
20.982342864 343
19.234355545 222
1.5567846 345
17.8767787 200

If I use the option -g or -n it works..but it is in ascending order. but I would like to display in descending order.I tried -nr but the Hello world line would be reverse as well as the last item.

My desired output would be
Hello world
20.982342864 343
19.234355545 222
17.8767787 200
1.5567846 345

Thanks.

Try...

awk 'NR==1; NR>1{print|"sort -rn"}' file1
# more file
Hello world
20.982342864 343
19.234355545 222
1.5567846 345
17.8767787 200

# sort -r file
Hello world
20.982342864 343
19.234355545 222
17.8767787 200
1.5567846 345

# echo $LC_CTYPE
en_US.UTF-8

sort -r -k 1,1 file

What you need to do is tell "sort" that you want to sort numerically instead of alphabetically: "1,2,13,20" is sorted numerically, alphabetically sorted it would look like "1,13,2,20", because "1" comes before "2" in the "alphabet" (the ASCII code) the sort command uses. Aphabetical sorting is the default, numerical sorting can bei achieved by using the option "-n" as you have already found out. If you want to reverse the sorting you have to use "-r" as you have already noticed too. So combine these two to get:

"cat file | sort -rn"

to get the desired result.

bakunin

# sort -rn file
20.982342864 343
19.234355545 222
17.8767787 200
1.5567846 345
Hello world

my output puts the "hello world" at the bottom, which is not what OP wants. is your output the same?