[wxPython] Sorting columns on a wxListCtrl

Hi,

Can anyone help me with sorting the columns of my list control? I've tried
copying the demo source as close as possible, but it still doesn't work right
for me. The event works and it does change the order, but it comes out
wrong. I don't truly understand how its supposed to work so can't tell what's
the problem is. Is there something special I have to load in the SetItemData
call?

#Here's where I load the control:

  #data is a list of strings
  for row in range( len(data) ):
      
    rowlist = string.split(data[row])
    self.ulist.InsertImageStringItem(row, rowlist[0],
        self.idx1)
    self.ulist.SetItemData(row, row)

    for column in range(1, len(rowlist) ):
      
      self.ulist.SetStringItem(row, column,
        rowlist[column])

#Here's where I set up the event and handler function:
  EVT_LIST_COL_CLICK(self, self.ulist.GetId(), self.OnColClick)
  
  def OnColClick(self, event):
    
    self.col = event.m_col
    self.ulist.SortItems(self.ColumnSorter)

  def ColumnSorter(self, key1, key2):
    
    if self.col == 0:
    
      item1 = self.ulist.GetItem(key1, self.col).GetText()
      item2 = self.ulist.GetItem(key2, self.col).GetText()
    else:
      item1 = string.atoi( self.ulist.GetItem(key1,
        self.col).GetText() )
      item2 = string.atoi( self.ulist.GetItem(key2,
        self.col).GetText() )
      
    #what's wrong?
    print "keys:", key1, key2, "items:", item1, item2

    if item1 == item2: print 0; return 0
    elif item1 < item2: print -1; return -1
    else: print 1; return 1

Mike Miller wrote:

Hi,

Can anyone help me with sorting the columns of my list control? I've tried
copying the demo source as close as possible, but it still doesn't work right
for me. The event works and it does change the order, but it comes out
wrong. I don't truly understand how its supposed to work so can't tell what's
the problem is. Is there something special I have to load in the SetItemData
call?

Indeed yes, the values passed to your ColumnSorter method are the
ItemData's of the two wxListItems, NOT the indexes.
Note that you may only store integers with SetItemData.
I suggest you store the original index in the list in ItemData and
use FindItemData to get the items for comparison.

···

--
Riaan >>> a='a=%s;a%%`a`';a%`a`
___________________________________________________
Boa Constructor - RAD GUI building IDE for wxPython
     http://boa-constructor.sourceforge.net