Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator

operator-keyword, pyqt, python, sorting

Solution

This may help you. Edit the regexp to match the digit patterns you're interested in. Mine will treat any digit fields containing `.` as floats. Uses `swapcase()` to invert your case so that `'A'` sorts after `'a'`.

Updated: Refined:

import re

def _human_key(key):
    parts = re.split('(\d*\.\d+|\d+)', key)
    return tuple((e.swapcase() if i % 2 == 0 else float(e))
            for i, e in enumerate(parts))

nums = ['9', 'aB', '1a2', '11', 'ab', '10', '2', '100ab', 'AB', '10a',
    '1', '1a', '100', '9.9', '3']
nums.sort(key=_human_key)

print '\n'.join(nums)

Output:

1
1a
1a2
2
3
9
9.9
10
10a
11
100
100ab
ab
aB
AB

Update: (response to comment) If you have a class `Foo` and want to implement `__lt__` using the `_human_key` sorting scheme, just return the result of `_human_key(k1) < _human_key(k2)`;

class Foo(object):

    def __init__(self, key):
        self.key = key

    def __lt__(self, obj):
        return _human_key(self.key) < _human_key(obj.key)

>>> Foo('ab') < Foo('AB')
True
>>> Foo('AB') < Foo('AB')
False

So for your case, you'd do something like this:

def __lt__(self, other):
    column = self.treeWidget().sortColumn()
    k1 = self.text(column)
    k2 = other.text(column)
    return _human_key(k1) < _human_key(k2)

The other comparison operators (`__eq__`, `__gt__`, etc) would be implemented in the same way.

Problem

I have data rows and wish to have them presented as follows: ``` 1 1a 1a2 2 3 9 9.9 10 10a 11 100 100ab ab aB AB ``` As I am using pyQt and code is contained within a TreeWidgetItem, the code I'm trying to solve is: ``` def __lt__(self, otherItem): column = self.treeWidget().sortColumn() #return self.text(column).toLower() < otherItem.text(column).toLower() orig = str(self.text(column).toLower()).rjust(20, "0") other = str(otherItem.text(column).toLower()).rjust(20, "0") return orig < other ```

Original source

Related problems