Alphabetical sorts

Neil Cerutti horpner at yahoo.com
Mon Oct 16 14:50:50 EDT 2006


On 2006-10-16, Ron Adam <rrr at ronadam.com> wrote:
>
> I have several applications where I want to sort lists in
> alphabetical order. Most examples of sorting usually sort on
> the ord() order of the character set as an approximation.  But
> that is not always what you want.

Check out strxfrm in the locale module.

>>> a = ["Neil", "Cerutti", "neil", "cerutti"]
>>> a.sort()
>>> a
['Cerutti', 'Neil', 'cerutti', 'neil']
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'English_United States.1252'
>>> a.sort(key=locale.strxfrm)
>>> a
['cerutti', 'Cerutti', 'neil', 'Neil']

-- 
Neil Cerutti



More information about the Python-list mailing list