[Pythoncard-users] case-insensitive sort added to util.py module
by Kevin Altis other posts by this author
Dec 20 2002 12:11AM messages near this date
[Pythoncard-users] FW: [wxPython-users] 2.3.4.2 is ready
|
[Pythoncard-users] wordwrap function added to util.py module
It seems like when I want to sort strings the comparison should always be
case-insensitive. After discussing this on a comp.lang.python thread today
I've checked in the following function which you may find useful. It does
not attempt to do a localized string compare, though that would be a nice
addition, but that could be a bit more complex because of the need to deal
with locale and do some string encodings in addition to using upper()
Here's an example of its usage:
> >> from PythonCardPrototype import util
> >> s = [u'ö', u'ä', u'Ä', 'b', 'a', 'B', u'a', 'A']
> >> # the default sort is very 1970's ;-)
> >> s.sort()
> >> s
['A', 'B', 'a', u'a', 'b', u'\xc4', u'\xe4', u'\xf6']
> >> s = util.caseinsensitive_sort(s)
> >> s
['A', 'a', u'a', 'B', 'b', u'\xc4', u'\xe4', u'\xf6']
You can use the same string for your own testing if you end up wanting to
jump into the locale module and play with strcoll and strxfrm and string
encoding conversions.
http://www.python.org/doc/current/lib/module-locale.html
Martin v. Löwis had another variation of the function below, but it required
Python 2.3's enumerate.
ka
---
def caseinsensitive_sort(list):
"""case-insensitive string comparison sort
doesn't do locale-specific compare
though that would be a nice addition
usage: list = caseinsensitive_sort(list)"""
tuplesList = [(x.upper(), x) for x in list]
tuplesList.sort()
return [x[1] for x in tuplesList]
-------------------------------------------------------
This SF.NET email is sponsored by: Geek Gift Procrastinating?
Get the perfect geek gift now! Before the Holidays pass you by.
T H I N K G E E K . C O M http://www.thinkgeek.com/sf/
_______________________________________________
Pythoncard-users mailing list
Pythoncard-users@[...].net
https://lists.sourceforge.net/lists/listinfo/pythoncard-users
|