Trouble sorting lists (unicode/locale related?)

Jeff Epler jepler at unpythonic.net
Sun Sep 21 10:25:38 EDT 2003


On Sun, Sep 21, 2003 at 02:10:16PM +0200, Peter Otten wrote:
> Try setting the appropriate locale first:
> 
> import locale
> locale.setlocale(locale.LC_ALL, ("no", None))
> 
> Then for a case-insensitive sort:
> 
> wordlist.sort(locale.strcoll)
> 
> should do (disclaimer: all untested).

If this did work, then you can use the DSU pattern like so:

decorated_wordlist = [(locale.strxfrm(w), w) for w in wordlist]
decorated_wordlist.sort()
wordlist[:] = [i[1] for i in decorated_wordlist]

from "man strxfrm"
DESCRIPTION
       The  strxfrm() function transforms the src string into a form such that
       the result of strcmp() on two strings that have been  transformed with
       strxfrm()  is  the  same  as the result of strcoll() on the two strings
       before their transformation.  The first n characters of the transformed
       string  are  placed  in  dest.  The transformation is based on the pro-
       gram’s current locale for category LC_COLLATE.  (See setlocale(3)).

Jeff





More information about the Python-list mailing list