[Note, 20130510: The posting below is correct, but I now maintain information in my Notes collection — see the current note or visit the search page for the Notes collection and search for "sort" or "insensitive", etc.]
This is tricky, because the sort-key key=str.lower
apparently only
works for ASCII strings, not Unicode strings. I get the error
TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
Instead, try declaring your locale and letting your LANG environment set your encoding for you:
import locale
locale.setlocale(locale.LC_ALL, '')
listname = sorted(listname, cmp=locale.strcoll)
which seems to work. My encoding is set to en_US.UTF-8
, which treats
A, a, and ā as the same letter; that's what I want for sorting in
this particular case.
I could also specify the encoding, but according to the Python docs at [http://docs.python.org/release/2.6.6/library/locale.html?highlight=locale#locale.setlocale][], it seems the actual encoding can be left out and supplied from one's environment.
That's what the empty single quotes are; but they need to be there
because setlocale()
takes a 2-tuple. Trying that on my system returns
'en_US.UTF-8'. If I omit the single quotes altogether, the function
returns 'C' and the sort is case sensitive.
http://docs.python.org/release/2.6.6/library/locale.html?highlight=locale#locale.setlocale