Hello, On 13/08/17 12:59 PM, Peng Yu wrote: > Hi, "B" is listed before "a". Is there a way to sort alphabetically > (as in an English dictionary)? (I think LC_* might need to be used, > but I am not sure what value it should be.) Thanks. > > $ printf '%s\n' a B c | sort > B > a > c >
There are two issues at hand: locale and case-sensitivity. You example appears to be in a C/POSIX locale where characters are sorted by their ASCII value. In this case, use "-f/--ignore-case" to ignore the upper/lower case: $ printf '%s\n' a B c | LC_ALL=C sort -f a B c In many other locales, the ordering (often referred to as "collation") already ignores upper/lower-case letters: $ printf '%s\n' a B c | LC_ALL=en_CA.UTF8 sort a B c Setting LC_ALL affects all locale variables. Use LC_COLLATE to change only the collation order (however this is likely to cause confusion). Example: LC_NUMERIC=en_US.UTF-8 LC_COLLATE=C sort If you are writing portable scripts and need consistent sorting order output regardless of the system's locale, it is best to set a specific locale (if you know it is available on the system), or always use "LC_ALL=C" optionally with "sort -f". Also see other sorting options such as -d/-b/-i which will affect ordering. regards, - assaf
