wxListCtrl SortItems compare?

BlitzMax Forums/Brucey's Modules/wxListCtrl SortItems compare?

MOBii(Posted 2016) [#1]
I never seen this before how dos it work?

wxListCtrl:
SortItems:Int(compare:Int(item1:Object, item2:Object, data:Object), data:Object)



Henri(Posted 2016) [#2]
Hi,

SortItems can be used to sort items in listctrl in any order (like alphabetically or numerically) and takes two parameters, name of the function which handles the comparing and data object which can be any type that you want to pass along (usually the listview reference itself, but can be anything).
'Example:

SortItems(myCompare, myListCtrl)


Comparing function is similar to comparing function found in TList and looks like this:
Function myCompare(obj1:Object, obj2:Object, data:Object)
	
	'Example sorting with text length (assuming objects are strings)
	Local s1:String = String(obj1)
	Local s2:String = String(obj2)
	
		
	If s1.length < s2.length Then
		Return - 1
	ElseIf s1.length > s2.length
		Return 1
	Else
		Return 0
	EndIf
	
EndFunction

The object in first parameters is the content of wxListItems client data field previously set with SetData. There are two options for setting the client data of each list item. One is to set it upon adding a new list item like:
Local li:wxListItem = New wxListItem.Create()
li.SetId(count)
li.SetImage(item.images[0])
li.SetText(item.items[0])
li.SetData(item)
InsertItem(li)
Second way is the iterate all items before calling SortItems() and set the client data then. Client data itself can be for example the text of the cell in column you are sorting.

-Henri