VB.Net – Sort Listview by any column

Video is ready, Click Here to View ×

Steps to sort vb.net listview by any column (dates, ints, string or doubles)


Public Class ListViewItemComparer
Implements IComparer

Private col As Integer
Private order As SortOrder

Public Sub New()
col = 0
order = SortOrder.Ascending
End Sub

Public Sub New(column As Integer, order As SortOrder)
col = column
Me.order = order
End Sub

Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare

Dim returnVal As Integer


‘ Attempt to parse the two objects as DateTime
Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
Dim secondDate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)

‘ Compare as date
returnVal = DateTime.Compare(firstDate, secondDate)

Catch ex As Exception

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.