Displaying integers in a list wxpython listctrl

I have a ListX Ctrl list with five columns. Four of these strings are held, the last one has integer values. I save them as strings (ie "4", "17", etc.). However, now that I have added the ColumnSorterMixin so that I can sort specific columns in the list, I find, of course, that the integer column is sorted lexically and not numerically.

Is there an easy way to do this?

+2


source to share


1 answer


I think the most reliable way to create a custom sort is to use SortItems () in wx.ListCtrl. Note that you need 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)

      

Haven't tested, 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.

+2


source







All Articles