sorting german characters äöü...

Ulrich Goebel ml at fam-goebel.de
Wed Oct 30 14:38:42 EDT 2013


Hello,

for a SQLite database I would like to prepare a collating function in 
python. It has to compare two (unicode-)strings s, t and should return 
-1 if s<t, 0 if s=t and 1 if s>t.

The strings are german names/words, and what I would like is to have a 
case-insensitive ordering, which treates

   ä as a
   ö as ö
   ü as ü
   ß as ss

and a few more things.

What I did is to "normalize" the two strings and then compare them:

def normal (s):
   r = s
   r = r.strip()   # wir entfernen führende und folgende Leerzeichen
   r = r.replace(u' ', '')    # wir entfernen alle Leerzeichen innerhalb
   r = r.replace(u'ß', u'ss')
   r = r.upper()   # alles in Großbuchstaben
   r = r.replace(u'Ä', u'A')   # Umlaute brauchen wir nicht...
   r = r.replace(u'Ö', u'O')   # "
   r = r.replace(u'Ü', u'U')   # "
   return r

def compare (a, b):
   aa = normal(a)
   bb = normal(b)
   if aa < bb:
     return -1
   elif aa == bb:
     return 0
   else:
     return 1

That works, but my be there is a more intelligent way? Especially there 
could be much more r.replace to handle all the accents as ^ ° ´ ` and so on.

Any ideas? That would be great!

Ulrich

-- 
Ulrich Goebel
Paracelsusstr. 120, 53177 Bonn



More information about the Python-list mailing list