1

Why do :

$ echo -e 'Q\ns\nV'  | sort 

outputs

Q
s
V

without changing the order of my list (taking in account the lower/uppercase ?)

4

In most languages, s sorts before V regardless of the case.

Sorting depends on localisation settings (LANG and LC_* variables).

You could use: LC_ALL=C sort if you wanted to sort according to the byte value order, but that may not do what you want if you're in a multi-byte locale.

If you want to sort in the order of your own language, but having uppercase letters before lowercase ones, you could do:

sed 's/./0&/g;s/0\([[:lower:]]\)/1\1/g' |
  sort |
sed 's/.\(.\)/\1/g'

That would cause lower-case letters to be sorted after every other character.

$ print -l Q s d é f D É F V | sort
d
D
é
É
f
F
Q
s
V

$ print -l Q s d é f D É F V | sed 's/./0&/g;s/0\([[:lower:]]\)/1\1/g' |
 sort |
 sed 's/.\(.\)/\1/g'
D
É
F
Q
V
d
é
f
s

That would only work in locales where collating elements are single characters only.

  • thank you, but I'm really surprised by the sentence "In most languages, s sorts before V regardless of the case.". That sounds weird :-) – Pierre Jan 21 '13 at 12:17
  • @Pierre. Does it? In the yellow pages in your country, are upper case names sorted before lower case ones? – Stéphane Chazelas Jan 21 '13 at 12:31
  • I meant, I didn't use the -i option, so I expected the uppercase to be sorted before the lowercase ... – Pierre Jan 21 '13 at 13:06
  • There are no "lower case names", "Jorge de la Puente" has "Puente" as last name. – vonbrand Jan 21 '13 at 16:41
3

sort's sort order depends on your environment's locale settings. From the sort manpage.

   *** 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.

The POSIX or C locale will make sort behave as expected:

reedm@www:~ $ echo -e 'Q\ns\nV' | LC_ALL='' sort 
Q
s
V
reedm@www:~ $ echo -e 'Q\ns\nV' | LC_ALL='' sort --ignore-case
Q
s
V
reedm@www:~ $ echo -e 'Q\ns\nV' | LC_ALL='c' sort 
Q
V
s
reedm@www:~ $ echo -e 'Q\ns\nV' | LC_ALL='c' sort --ignore-case
Q
s
V

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.