Hey all,
I have a listbox that sorts fine with regular numbers. One column has numbers but with commas (it’s Earnings, so… money). Is there a foolproof way to sort the regular whole numbers and the numbers with commas properly? The column with commas doesn’t always sort right.
Use the Listbox.CompareRows event to write your own sorting code. Also, instead of comparing the string values of the cells (and worrying about commas, etc.), store the money amount as a numeric type in the CellTag of the cell:
myListbox.AddRow("$1,234")
myListbox.CellTag(myListbox.LastIndex, 0) = 1234[/code]
and then in the CompareRows event:
[code]Function CompareRows(row1 as Integer, row2 as Integer, column as Integer, ByRef result as Integer) As Boolean
Dim value1 As Integer = Me.CellTag(row1, column)
Dim value2 As Integer = Me.CellTag(row2, column)
If value1 = value2 Then
result = 0
ElseIf value1 > value2 Then
result = 1
Else
result = -1
End If
Return True
End Function
Works. Thanks, Andrew!