V
vb newbie
Does anybody have suggestions on how to programatically sort a ListView
on TWO subitem columns?
Here's the code I'm using to sort on one subitem:
Public Function Compare(ByVal x As Object, ByVal y As _
Object) As Integer Implements _
System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, _
ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, _
ListViewItem)
' Get the sub-item values.
Dim string_x As String
If item_x.SubItems.Count <= m_ColumnNumber Then
string_x = ""
Else
string_x = item_x.SubItems(m_ColumnNumber).Text
End If
Dim string_y As String
If item_y.SubItems.Count <= m_ColumnNumber Then
string_y = ""
Else
string_y = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them based on column types
Select Case (Form1.sortTypes(m_ColumnNumber))
Case "c"
If m_SortOrder = SortOrder.Ascending Then
Return String.Compare(string_x, string_y)
Else
Return String.Compare(string_y, string_x)
End If
Case "d"
If m_SortOrder = SortOrder.Ascending Then
Return Date.Compare(string_x, string_y)
Else
Return Date.Compare(string_y, string_x)
End If
Case "n"
If m_SortOrder = SortOrder.Ascending Then
Return Decimal.Compare(string_x, string_y)
Else
Return Decimal.Compare(string_y, string_x)
End If
End Select
End Function
on TWO subitem columns?
Here's the code I'm using to sort on one subitem:
Public Function Compare(ByVal x As Object, ByVal y As _
Object) As Integer Implements _
System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, _
ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, _
ListViewItem)
' Get the sub-item values.
Dim string_x As String
If item_x.SubItems.Count <= m_ColumnNumber Then
string_x = ""
Else
string_x = item_x.SubItems(m_ColumnNumber).Text
End If
Dim string_y As String
If item_y.SubItems.Count <= m_ColumnNumber Then
string_y = ""
Else
string_y = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them based on column types
Select Case (Form1.sortTypes(m_ColumnNumber))
Case "c"
If m_SortOrder = SortOrder.Ascending Then
Return String.Compare(string_x, string_y)
Else
Return String.Compare(string_y, string_x)
End If
Case "d"
If m_SortOrder = SortOrder.Ascending Then
Return Date.Compare(string_x, string_y)
Else
Return Date.Compare(string_y, string_x)
End If
Case "n"
If m_SortOrder = SortOrder.Ascending Then
Return Decimal.Compare(string_x, string_y)
Else
Return Decimal.Compare(string_y, string_x)
End If
End Select
End Function