2
votes

I have a wxPython ListCtrl with five columns. Four of these hold strings, the last one has integer values. I have been storing these as strings (i.e. '4', '17', etc.). However, now that I have added a ColumnSorterMixin to let me sort specific columns in the list, I'm finding, of course, that the integer column is being sorted lexically rather than numerically.

Is there a simple way of fixing this?

1

1 Answers

2
votes

I think that the most robust way of doing custom sort is to use SortItems() function in wx.ListCtrl. Note that you have to provide item data for each item (using SetItemData())

Just provide your own callback, say:

def sortColumn(item1, item2):
    try: 
        i1 = int(item1)
        i2 = int(item2)
    except ValueError:
        return cmp(item1, item2)
    else:
        return cmp(i1, i2)

Didn't check it, but something along these lines should work for all columns, unless you have a column where some values are strings representing integers and some are not.