ListBox.CompareRows

From Xojo Documentation

Event


ListBox.CompareRows(Row1 as Integer, Row2 as Integer, Column as Integer, ByRef Result as Integer) As Boolean

Supported for all project types and targets.

The CompareRows event is used for sorting a column of a ListBox in a manner that is not provided by the default mechanism. Row and Column are zero-based. 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 ListBox 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.

Example

Suppose your ListBox contains a first column (numbered 0) which contains a string value. You can let the Listbox 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 RowCompared event as follows:

Function CompareRows(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.CellValueAt(row1, column )) < Val(Me.CellValueAt(row2, column)) Then
result = -1
ElseIf Val(Me.CellValueAt(row1, column)) > Val(Me.CellValueAt(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 CompareRows(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.CellValueAt(row1, column)) - Val(Me.CellValueAt( 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 CompareRows(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