cglacet
cglacet

Reputation: 10912

Bash sorting fails sometimes

Here is the problem :

echo -e " 1.0101010101010102 Ok \n 
     1.00 Ok \n 
     1.010204081632653 Ok \n 
     1.010752688172043 Ok \n 
     1.0 FAIL \n 
     1.010989010989011 Ok" 
| sort -n

returns

 1.00 Ok 
 1.0101010101010102 Ok 
 1.010204081632653 Ok 
 1.010752688172043 Ok 
 1.010989010989011 Ok
 1.0 FAIL 

Apparently the bug occurs only with the "i.0"-form values. It also has the same weird result with sorting option "-g".

Does anyone has any clue about why it does that, and how to correct the sorting... ?

Upvotes: 1

Views: 254

Answers (1)

rici
rici

Reputation: 241671

If you are using GNU sort, you need to use -g rather than -n to sort floating point values. -n is only for integers.

Most probably, your problem is that your locale is not set to the C locale. Many locale settings interfere with numeric sort, particularly of floating point numbers (where some locales expect to see comma as a decimal separator). Try this:

LC_COLLATE= sort -g

If you're using a different sort, you'll need to read its documentation.

Upvotes: 4

Related Questions