sorting german characters äöü...

U

Ulrich Goebel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top