localizing a sort

Ricardo Aráoz ricaraoz at gmail.com
Sat Sep 1 23:56:38 CEST 2007


Hi, I've been working on sorting out some words.

My locale is :
>>> import locale
>>> locale.getdefaultlocale()
('es_AR', 'cp1252')

I do :
>>> a = 'áéíóúäëïöüàèìòù'
>>> print ''.join(sorted(a, cmp=lambda x,y: locale.strcoll(x,y)))
aeiouàáäèéëìíïòóöùúü

This is not what I am expecting. I was expecting :
aáàäeéèëiíìï.....etc.

The reason is that if you want to order some words (say for a dictionary
(paper dict, where you look up words)) this is what happens :
>>> a = 'palàbra de pàlabra de pblabra'
>>> print ' '.join(sorted(a.split(), cmp=lambda x,y: locale.strcoll(x, y)))
de de palàbra pblabra pàlabra

While any human being would expect :

de de palàbra pàlabra pblabra

Does anybody know a way in which I could get the desired output?

TIA







More information about the Python-list mailing list