[wxPython] wxListCtrl sorting help

Hi,

I've implemented sorting of the ListCtrls on my app, and it seems to work just
like in the demo. The problem is that its just too slow. When there are 50
or so items it's fine but if I have 500 or so (very common) it literally takes
2 MINUTES(!) after I click the column header before my app comes back to
life.

Here's an example of the code I am using. I'm wondering if I can bolt in my
own sorting algorithm to speed things up? Is there a way to reduce the
function calls I am making? Please help =). I've already made two functions
to separate the string sorts from the numerical sorts to speed things up
slightly. the sortdirection variables let me reverse the sort on the second
col click.

def ColumnSorterNumber(self, key1, key2):

  item1 = string.atoi( self.GetItem( self.FindItemData(-1, key1), self.col
).GetText() )
  item2 = string.atoi( self.GetItem( self.FindItemData(-1, key2), self.col
).GetText() )

  if item1 == item2: return 0
  elif item1 < item2: return self.nsortdirection
  else: return self.sortdirection

def ColumnSorterString(self, key1, key2):

  item1 = self.GetItem( self.FindItemData(-1, key1), self.col ).GetText()
  item2 = self.GetItem( self.FindItemData(-1, key2), self.col ).GetText()
    
  if item1 == item2: return 0
  elif item1 < item2: return self.nsortdirection
  else: return self.sortdirection

···

----------------------------------------------------------------------------

Hi Mike,

Mike Miller wrote:

Hi,

I've implemented sorting of the ListCtrls on my app, and it seems to work just
like in the demo. The problem is that its just too slow. When there are 50
or so items it's fine but if I have 500 or so (very common) it literally takes
2 MINUTES(!) after I click the column header before my app comes back to
life.

Here's an example of the code I am using. I'm wondering if I can bolt in my
own sorting algorithm to speed things up? Is there a way to reduce the
function calls I am making? Please help =). I've already made two functions
to separate the string sorts from the numerical sorts to speed things up
slightly. the sortdirection variables let me reverse the sort on the second
col click.

def ColumnSorterNumber(self, key1, key2):

  item1 = string.atoi( self.GetItem( self.FindItemData(-1, key1), self.col
).GetText() )
  item2 = string.atoi( self.GetItem( self.FindItemData(-1, key2), self.col
).GetText() )

  if item1 == item2: return 0
  elif item1 < item2: return self.nsortdirection
  else: return self.sortdirection

def ColumnSorterString(self, key1, key2):

  item1 = self.GetItem( self.FindItemData(-1, key1), self.col ).GetText()
  item2 = self.GetItem( self.FindItemData(-1, key2), self.col ).GetText()

  if item1 == item2: return 0
  elif item1 < item2: return self.nsortdirection
  else: return self.sortdirection

----------------------------------------------------------------------------

I just timed the column sorting I use in Boa's Profile view.
For me it takes less than a second (0.2 - 0.5 secs) to sort 850+ items
on
an AMD K6 450 running WinNT and 2.2.0.

Do you only store the data in the ListCtrl or do you have it in lists
aswell?
This is the only difference that I see between our approaches.

def ColumnSorterNumber(self, key1, key2):

I use key1 and key2 to look up the values being compared directly from
a list, not looking them up from the ListCtrl.

There might also be speed differences between wxGTK and wxMSW.

Hope this helps

···

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