[Tutor] Fw: utf locale sorting

Kent Johnson kent37 at tds.net
Wed Sep 16 19:06:32 CEST 2009


On Wed, Sep 16, 2009 at 11:45 AM, Igor Mavrović - Maqui at IRB
<igor.mavrovic at irb.hr> wrote:
> Sorry Kent, I should have put it in the original message...
> This is the way I call the sorted function:
>
>   # resultSet looks like: [[("DN", {"":["", ...], ...})], ...]
>
>   resultSetSortedByCn = sorted(resultSet, key=lambda x:(x[0][1]["sn"],
> x[0][1]["givenName"]))
>   resultSetSortedByOu = sorted(resultSet, key=lambda x:(x[0][1]["ou"],
> x[0][1]["sn"], x[0][1]["givenName"]))
>
> I have to use it like this for obvious reasons (sort by org-unit, surname
> and then name). Therefore Rich's suggestion:
>
>   print sorted(words, key=lambda o: locale.strxfrm(o[0]))
>
> can't work 'cause strxfrm's argument must be a string, not a tuple...

How about this (assuming both arguments need to be transformed)?
key=lambda x:(locale.strxfrm(x[0][1]["sn"]),
locale.strxfrm(x[0][1]["givenName"]))

Kent


More information about the Tutor mailing list