於是,我們就在專案裡新增一個class來實作IComparer這個interface:
Public Class Mysort : Implements IComparer
Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.CompareDim c1 As Integer = Integer.Parse(CType(a, ListViewItem).SubItems(0).Text)
Dim c2 As Integer = Integer.Parse(CType(b, ListViewItem).SubItems(0).Text)
If (c1 > c2) ThenReturn 1
End If
If (c1 < c2) ThenReturn -1
ElseReturn 0
End If
End Function
End Class
然後,把要排序的ListView,裡面的排序準則設成這個class的一個實體,再呼叫Sort,就會依我們方式來排序了
Dim mysort As New Mysort
Me.ListView1.ListViewItemSorter = mysort
Me.ListView1.Sort()