Linux sort doesn't work with negative float numbers

Go To StackoverFlow.com

10

How to sort this kind of input?

0.00159265291648695254
-0.00318530179313823899
0
0.00999983333416666468
0.00362937767285478371
0.00477794259012844049
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
-0.00522201549675090458

Either sort -n data and sort -g data procudes this:

0
0.00159265291648695254
-0.00318530179313823899
0.00362937767285478371
0.00477794259012844049
-0.00522201549675090458
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
0.00999983333416666468

On the other hand -1.whatever would be in front of the zero. I need the sort to notice the minus signs. Thank you.

2012-04-05 15:05
by tsusanka
What flavor of UNIX are you on? My Ubuntu sorts just fine using both -g and -n - Joachim Isaksson 2012-04-05 15:09
well that's bizarre, i'm using as well ubuntu (Ubuntu 11.10 - tsusanka 2012-04-05 15:17
I'm running 11.10 too. My sort gives your exact output without any parameters using /usr/bin/sort but both -g and -n work - Joachim Isaksson 2012-04-05 15:21
please type sortkev 2012-04-05 15:25
i tried that.. sort is /usr/bin/sort how to obtain the sort version - tsusanka 2012-04-05 15:28
sort --versionzwol 2012-04-05 15:37
I just don't get it, my friend's kubuntu says the same.. http://pastebin.com/zpYkrzn - tsusanka 2012-04-05 15:37
Try it like this: printf '0.03\n0.4\n-0.3\n0\n' | sort -n Your pastebin looks like the greater-than signs actually got into the sort input somehow - zwol 2012-04-05 15:39
sort (GNU coreutils) 8.5, looks like this could be something with local settings, i have to check it ou - tsusanka 2012-04-05 15:40


21

All those troubles did my local settings. My ubuntu is in Czech:

$ echo $LANG
cs_CZ.UTF-8

In this local setting it's not a decimal point, rather a decimal comma that seperates integer from the rest (as we were thought in math classes, in our language we really do write comma instead of a point).

Therefore:

echo '0,03 >> 0,4 >
> -0,3 >
> 0' | sort -n
> 0
> -0,3 >
> 0,4 >
0,03 >

If you are writing a bash script, set the sorting routine to use the "normal" settings.

export LC_ALL=C
2012-04-05 15:48
by tsusanka


2

The problem may be in your sort command. If I run the same my result is as expected:

$ echo '0.00159265291648695254
> -0.00318530179313823899
> 0
> 0.00999983333416666468
> 0.00362937767285478371
> 0.00477794259012844049
> -0.00637057126765263261
> 0.00681464007477014026
> -0.00840724736714870645
> -0.00522201549675090458' | sort -n
-0.00840724736714870645
-0.00637057126765263261
-0.00522201549675090458
-0.00318530179313823899
0
0.00159265291648695254
0.00362937767285478371
0.00477794259012844049
0.00681464007477014026
0.00999983333416666468

You shoud use GNU sort if not using it

sort (GNU coreutils) 5.93
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.
2012-04-05 15:15
by Diego Torres Milano
my version is even newer, so wier - tsusanka 2012-04-05 15:39
thx for your comment, see my anwser if intereste - tsusanka 2012-04-05 15:49
Ads