Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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


I got:


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


And the output is:


Anybody can explain this? By the way, what is 'directory-order' when using sord -d?

share|improve this question

migrated from stackoverflow.com Jul 19 '12 at 11:40

This question came from our site for professional and enthusiast programmers.

sort order depends on your locale settings –  janneb Jul 19 '12 at 6:20

2 Answers 2

up vote 4 down vote accepted

Looks like you are using a non-POSIX locale.


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 theLC_ALL' environment variable to C'.In that case, set theLC_ALL' environment variable to C'. Note that setting onlyLC_COLLATE' has two problems. First, it is ineffective if LC_ALL' is also set. Second, it has undefined behavior ifLC_CTYPE' (or LANG', ifLC_CTYPE' is unset) is set to an incompatible value. For example, you get undefined behavior if LC_CTYPE' isja_JP.PCK' but LC_COLLATE' isen_US.UTF-8'.

share|improve this answer
The OP is asking what the sort order is, not how to change it. –  Gabe Jul 19 '12 at 6:25
Thanks, I've tested on my machine and locale settings do affect sort behavior –  old_bear Jul 19 '12 at 8:01

To determine the sort order, simply create a file with a different character on each line and the sort it. The resulting output will tell you the sort order.

share|improve this answer
Nice, simple and efficient –  old_bear Jul 19 '12 at 8:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.