DesktopListBox.RowComparison

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Event


DesktopListBox.RowComparison(Row1 as Integer, Row2 as Integer, Column as Integer, ByRef Result as Integer) As Boolean

New in 2021r3

Supported for all project types and targets.

The RowComparison event is used for sorting a column of a DesktopListBox in a manner that is not provided by the default mechanism. The result of the comparison is returned in the last parameter, Result, which is declared Byref. The default mechanism sorts cell values lexicographically. If you implement the event, it gets called during a DesktopListBox sort, e.g., when a user clicks in the header area.

Notes

Parameters:

  • Row1: Row number of one of the rows being compared.
  • Row2: Row number of the other row being compared.
  • Column: Number of column being sorted.

Set Result to:

  • 0: If items in Row1 and Row2 in specified column are equal.
  • -1: Contents of Row1 < Contents of Row2.
  • 1: Contents of Row1 > Contents of Row2.

Return True if the returned Result parameter is accurate for sorting.

Return False if you want the Listbox to use the default lexicographic sorting of the column.

Sample Code

Suppose your DesktopListBox contains a first column (numbered 0) which contains a string value. You can let the DesktopListBox use the default lexicographic comparison for such a column. However, the second column (numbered 1) contains a numerical value that should be sorted as such. You can implement the RowComparison event as follows:

Function RowComparison(row1 As Integer, row2 As Integer, column As Integer, ByRef result As Integer) As Boolean
Select Case column
Case 0 // This is a string column. Let the listbox manage it by returning false
Return False

Case 1 // This is our numerical value column. Let's do the work ourselves
If Val(Me.CellTextAt(row1, column )) < Val(Me.CellTextAt(row2, column)) Then
result = -1
ElseIf Val(Me.CellTextAt(row1, column)) > Val(Me.CellTextAt(row2, column)) Then
result = 1
Else
result = 0
End If
Return True

Else //some other column for which we let the listbox handle comparison
Return False
End Select
End Function

or, more simply, using the Sign function:

Function RowComparison(row1 As Integer, row2 As Integer, column As Integer, ByRef result As Integer) As Boolean
Select Case column
Case 0 // This is a string column. Let the listbox manage it by returning false
Return False

Case 1 // This is our numerical value column. Let's do the work ourselves
result = Sign(Val(Me.CellTextAt(row1, column)) - Val(Me.CellTextAt( row2, column)))
Return True

Else //some other column for which we let the listbox handle comparison
Return False
End Select
End Function

To sort a column containing checkboxes use

Function RowComparison(row1 As Integer, row2 As Integer, column As Integer, ByRef result As Integer) As Boolean
// Column 0 contains checkboxes.
// We want to sort it by checkbox value, with unchecked rows before checked rows.

Select Case column
Case 0 // column 0 contains our checkboxes
If Me.CellCheckBoxValueAt(row1, column) XOr Me.CellCheckBoxValueAt(row2, column) Then
// CellCheckBox values are unequal. If row1 is true, then row2 must be true and vice versa.
If Me.CellCheckBoxValueAt(row1, column) Then
// row1 < row2
result = 1
Else
// row1 < row2
result = -1
End If
Else
// CellCheckBox values are equal, so row1 = row2.
result = 0
End If
Return True

Else
// let the listbox do default comparison
Return False
End Select
End Function