qiuxiafei
qiuxiafei

Reputation: 5997

Why does the sort command sort differently if there are trailing fields?

Considering the sort key: a, a01 and a02, if there's no trailing fields, the sort result looks like:

$ cat test1
a01
a
a02
$ sort test1
a
a01
a02
$

But if there are tailing fields, something goes strange:

$ cat test2
a01 7
a 12
a02 42
$ sort test2
a01 7
a02 42
a 12
$

Why does the key "a" fall from the top to the bottom of the sort result?

My sort version is "sort (GNU coreutils) 5.97".

Upvotes: 0

Views: 138

Answers (1)

larsks
larsks

Reputation: 312610

The man page for my version of sort says:

***  WARNING  *** The locale specified by the environment affects sort order.  
Set LC_ALL=C to get the traditional sort order that uses native byte values.

And indeed, if I set LC_ALL=C and run sort on your second example, I get:

$ LC_ALL=C sort < tosort 
a 12
a01 7
a02 42

Your default locate is probably something other than C.

Upvotes: 1

Related Questions