I have a function in python which returns a tuple of a given key for the Natural-Sort/Human algorithm.
See fake _human_key.
But I need this to change this to replace German umlauts by their standard alphabetical characters.
Long story short, I want to get rid of Ä, Ö, Ü, ß
for the sorting.
Also, the case should not be considered. A small d
should have the same priority as a capital D
...
For the umlauts I am utilizing the replace-function which seems a pretty awkward way to do it... :-/ I have no better idea... Any suggestions?
Also I am not able to rewrite this to get rid of the case sensitiveness...
So far I have:
def _human_key(key):
key = key.replace("Ä", "A").replace("Ö", "O").replace("Ü", "U")\
.replace("ä", "a").replace("ö", "o").replace("ü", "u")\
.replace("ß", "s")
parts = re.split(r'(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e))
for i, e in enumerate(parts))
return parts
Examples: I have the values
Zabel
Schneider
anabel
Arachno
Öztürk
de 'Hahn
which I want to sort; currently this puts:
anabel
de 'Hahn
Arachno
Öztürk
Schneider
Zabel
because the small characters a treated with priority...
Expectation:
anabel
Arachno
de 'Hahn ( <-- because "d" comes after "a")
Öztürk
Schneider
I feel the replace is not the right way to achieve the problem with the umlauts, but can't find a better solution.
Update/Background information:
I am calling this from outside, from the class "QSortFilterProxyModel", I need this for sorting rows according to their clicked columns. I have a QTreeView whichs displays a result set from the database, and one column contains german family names, that's the background.
class HumanProxyModel(QtCore.QSortFilterProxyModel):
def lessThan(self, source_left, source_right):
data_left = source_left.data()
data_right = source_right.data()
if type(data_left) == type(data_right) == str:
return _human_key(data_left) < _human_key(data_right)
return super(HumanProxyModel, self).lessThan(source_left, source_right)