[Moin-devel] case insensitive title index suggestion
Magnus Lyckå
magnus at thinkware.se
Thu May 29 17:00:04 EDT 2003
At 13:12 2003-05-29 +0200, Karolina Lindqvist wrote:
>I have pages with start both with and upper case initial and a lower case.
>That causes the title index to have two groups for each letter, one for the
>upper case version and one for the lower case. A nice feature would to make
>the title index case-insensitive.
>
>As a stopgap, I changed the sort() call in _macro_TitleIndex() to use the
>following caseinsensitive_sort() function, which I found on the net, and it
>(+ one letter = letter.upper()) does the job
>
>def caseinsensitive_sort(list):
> tuplesList = [(x.upper(), x) for x in list]
> tuplesList.sort()
> return [x[1] for x in tuplesList]
>
>This is supposedly faster than using a compare function to sort().
Yes, using l.sort(some_python_function) is slow, since it the fast
C-based sort function has to call the Python function over and over
again. But your sort won't do the right thing, even it its better
than a plain sort.
>>> l = ['Hej', 'allan', 'åke', 'Östen', 'Ärling']
>>> l.sort()
>>> for n in l: print n,
...
Hej allan Ärling Östen åke
>>> l.sort(lambda x, y: cmp(x.upper(), y.upper()))
>>> for n in l: print n,
...
allan Hej Ärling åke Östen
>>>
At least 'a' came before 'H' now, but 'å' should come before 'Ä'
in Sweden.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'Swedish_Sweden.1252'
>>> l.sort(locale.strcoll)
>>> for n in l: print n,
...
allan Hej åke Ärling Östen
The schwartzian transform is still useful, but the locale aware version
would be (untested):
import locale
locale.setlocale(locale.LC_ALL, '')
...
def locale_aware_sort(list):
tuplesList = [(locale.strxfrm(x), x) for x in list]
tuplesList.sort()
return [x[1] for x in tuplesList]
--
Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program
More information about the Moin-devel
mailing list