Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a ListView on subitems....please help 2

Status
Not open for further replies.

herb8

Programmer
Sep 4, 2001
45
US
Im trying to sort a listview on subitems and cant seem to figure it out. I have read some about the icomparer which I also have yet to conquer. Any help would be appreciated.
Thanks, Matt
 
Private Sub ZipListView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ZipListView.ColumnClick
ColumnClick(e.Column)
End Sub
Public Sub ColumnClick(ByVal ColumnHeader As Integer)
If ZipListView Is Nothing Then Exit Sub
With ZipListView
'.SortKey = ColumnHeader.Index - 1
If intSortOrder = SortOrder.Ascending Then
intSortOrder = SortOrder.Descending
Else
intSortOrder = SortOrder.Ascending
End If
.ListViewItemSorter = New ListViewItemComparer(ColumnHeader, intSortOrder)
' Call the sort method to manually sort.
.Sort()
End With
End Sub
' Embedded Class
Class ListViewItemComparer
Implements IComparer
Private mintcol As Integer
Private mintSortOrder As System.Windows.Forms.SortOrder = SortOrder.Ascending
Private mblnIgnoreCase As Boolean
Public Sub New()
mintcol = 0
mintSortOrder = SortOrder.Ascending
mblnIgnoreCase = False
End Sub

Public Sub New(ByVal column As Integer, _
ByVal SortOrder As System.Windows.Forms.SortOrder, _
Optional ByVal IgnoreCase As Boolean = False)
mintcol = column
mintSortOrder = SortOrder
mblnIgnoreCase = IgnoreCase
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim returnVal As Integer = -1

returnVal = [String].Compare(CType(x, _
ListViewItem).SubItems(mintcol).Text, _
CType(y, ListViewItem).SubItems(mintcol).Text, True)

If mintSortOrder = SortOrder.Descending Then returnVal = -returnVal
Return returnVal
End Function
End Class Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks John! Great tip and useful piece of code, I went ahead passed the ListView into the columnclick sub so I can utilize it with all my listviews. Thanks again,
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top