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.

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 ?)

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
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
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

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

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