Default Order of Linux Sort Command

linuxlocalesort

For a long period I thought the default behavior of the sort program was using ASCII order. However, when I input the following lines into sort without any arguments:

#
@

I got:

@
#

But according to the ASCII table, # is 35 and @ is 64. Another example is:

A
a

And the output is:

a
A

Can anybody explain this? By the way, what is 'dictionary-order' when using sort -d?

Best Answer

Looks like you are using a non-POSIX locale.

Try:

export LC_ALL=C

and then sort.

info sort clearly says:

(1) If you use a non-POSIX locale (e.g., by setting `LC_ALL' to `en_US'), then `sort' may produce output that is sorted differently than you're accustomed to. In that case, set the `LC_ALL' environment variable to `C'. Note that setting only `LC_COLLATE' has two problems. First, it is ineffective if `LC_ALL' is also set. Second, it has undefined behavior if `LC_CTYPE' (or `LANG', if `LC_CTYPE' is unset) is set to an incompatible value. For example, you get undefined behavior if `LC_CTYPE' is `ja_JP.PCK' but `LC_COLLATE' is `en_US.UTF-8'.

Related Question