[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to sort alphabetically?
From: |
Assaf Gordon |
Subject: |
Re: How to sort alphabetically? |
Date: |
Sun, 13 Aug 2017 13:15:20 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
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