case-insensitive and internationalized sort

Bengt Richter bokr at oz.net
Thu Dec 19 20:45:10 EST 2002


On Thu, 19 Dec 2002 22:26:08 +0100, =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= <martin at v.loewis.de> wrote:
[...]

>It turns out that the builtin comparison of such tuples does what you
>want:
>   cmp ((x1upper, x1),(x2upper,x2)) is cmp(x1upper,x2upper) if this
>   is nonzero, and is cmp(x1,x2) if both upper-case strings compare
>   equal
>
>So this will sort the list in a way that is case insensitive; if two
>strings differ only in case, it will sort the strings in a 
>case-sensitive manner.

This gives me an idea ;-) You could just use a dictionary of all possible
(~256**2 or <number of printables>**2) pair tuples and assign values -1, 0, or 1
to be the cmp values. Then if you got a change to make the builtin dict callable,
so that d(x,y) == d[(x,y)], then you could pass a dict as the cmp function to a
sort with minimum overhead. (You can obviously wrap up something to do the same now,
but it wouldn't be as fast).

Regards,
Bengt Richter



More information about the Python-list mailing list