[Tutor] Fw: utf locale sorting

Kent Johnson kent37 at tds.net
Wed Sep 16 16:33:34 CEST 2009


On Wed, Sep 16, 2009 at 9:51 AM, Igor Mavrović - Maqui at IRB
<igor.mavrovic at irb.hr> wrote:
> Hi,
>
> I know about the use of locale module:
>
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "hr_HR.UTF8")
>>>> print sorted(words, key=locale.strxfrm)
>
> but I have specific and complicated data structure (list of lists containing
> strings, tuples and dictionaries) due to LDAP search result data.
> So I use the 'key' option to get to the neaded attributes, and can't use it
> for the locale setting. How can I sort this monster list by attribute values
> inside it, and still get the correct sorting order?

The key= parameter can be any function. What are you using now? Just
add the call to locale.strxfrm() to your key function. If you are
currently using operator.itemgetter or operator.attrgetter you will
have to change to explicit attribute access.

Kent


More information about the Tutor mailing list