Sorting ListView by multiple columns in C #
With the controls, ListView
you can specify the column to sort, as well as the method sort()
whenever you want.
However, this only allows sorting by one column.
I try to sort, say first column A and then column F if they are the same.
I found several custom comparison classes written online, but wondered if stackoverflow could show a cleaner way. Also, it might help others looking for it in the future :)
Any suggestions or examples on how to do this are appreciated.
source to share
So, after the game, I came up with a proposal to write a class ListViewItemComparer
through an interface IComparer
.
Then I overwritten the method Compare()
and could now return -1, 0, or 1 depending on the comparison of the first primary column and then when the secondary column is equal.
Quite possibly in the end, I think.
source to share
As with almost all tasks, ObjectListView (an open source wrapper around the .NET WinForms ListView) makes life a lot easier with a ListView.
ObjectListView has properties SecondarySortColumn
and SecondarySortOrder
to do what you ask.
If you want to make sorting even more convenient, you can install CustomSorter
. Check out this recipe
source to share
@MarkMayo, I created my own sorter class ListViewItemComparer
via an interface IComparer
that supports sorting secondary / priority columns.
I am rewriting the method Compare()
to support string comparison, date comparison and case insensitivity.
You will first sort the desired column, if both values ββbeing compared are the same, the second column will be used for sorting and therefore secondary sorting.
You just need to enable this sorter class and change the Form Listview event ColumnClick
with the following VB.Net sample code:
ListViewItemComparer Class
Imports System.Collections
''' <summary>
''' This class is an implementation of the 'IComparer' interface.
''' </summary>
Public Class ListViewColumnSorter
Implements IComparer
''' <summary>
''' Specifies the column to be sorted
''' </summary>
Private ColumnToSort As Integer
''' <summary>
''' Specifies the secondary column to be sorted
''' </summary>
Private SecondaryColumnToSort As Integer = -1
''' <summary>
''' Specifies the order in which to sort (i.e. 'Ascending').
''' </summary>
Private OrderOfSort As SortOrder
''' <summary>
''' Class constructor. Initializes various elements
''' </summary>
Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
ColumnToSort = column_number
OrderOfSort = sort_order
End Sub
''' <summary>
''' Class constructor. Initializes various elements
''' </summary>
Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder, ByVal secondary_column_number As Integer)
ColumnToSort = column_number
SecondaryColumnToSort = secondary_column_number
OrderOfSort = sort_order
End Sub
''' <summary>
''' This method is inherited from the IComparer interface. It compares the two objects passed and support secondary column comparison
''' </summary>
''' <param name="x">First object to be compared</param>
''' <param name="y">Second object to be compared</param>
''' <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Dim compareResult As Integer
Dim listviewX As ListViewItem, listviewY As ListViewItem
' Cast the objects to be compared to ListViewItem objects
listviewX = DirectCast(x, ListViewItem)
listviewY = DirectCast(y, ListViewItem)
' Compare the two items
Dim x1 As Object = listviewX.SubItems(ColumnToSort)
Dim y1 As Object = listviewY.SubItems(ColumnToSort)
' Use .tag for comparison if not empty
If (x1.Tag IsNot vbNullString) And (y1.Tag IsNot vbNullString) Then
compareResult = ObjectComparer(x1.Tag, y1.Tag)
Else
compareResult = ObjectComparer(x1.Text, y1.Text)
End If
'require secondary column compare?
If (compareResult = 0 And SecondaryColumnToSort >= 0 And SecondaryColumnToSort <> ColumnToSort) Then
' Compare the two items
Dim x2 As Object = listviewX.SubItems(SecondaryColumnToSort)
Dim y2 As Object = listviewY.SubItems(SecondaryColumnToSort)
' Use .tag for comparison if not empty
If (x2.Tag IsNot vbNullString) And (y2.Tag IsNot vbNullString) Then
compareResult = ObjectComparer(x2.Tag, y2.Tag)
Else
compareResult = ObjectComparer(x2.Text, y2.Text)
End If
End If
' Calculate correct return value based on object comparison
If OrderOfSort = SortOrder.Ascending Then
' Ascending sort is selected, return normal result of compare operation
Return compareResult
ElseIf OrderOfSort = SortOrder.Descending Then
' Descending sort is selected, return negative result of compare operation
Return (-compareResult)
Else
' Return '0' to indicate they are equal
Return 0
End If
End Function
''' <summary>
''' This method compares the two objects passed. Object supported are numeric, dates and string
''' </summary>
''' <param name="x">First object to be compared</param>
''' <param name="y">Second object to be compared</param>
''' <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
Private Function ObjectComparer(x As Object, y As Object) As Integer
Dim compareResult As Integer
If IsNumeric(x) And IsNumeric(y) Then 'comparing numbers
compareResult = Val(x).CompareTo(Val(y))
ElseIf IsDate(x) And IsDate(y) Then 'comparing dates
compareResult = DateTime.Parse(x).CompareTo(DateTime.Parse(y))
Else 'comparing string
Dim ObjectCompare As New CaseInsensitiveComparer
compareResult = ObjectCompare.Compare(x.ToString, y.ToString)
End If
Return compareResult
End Function
End Class
Windows Form Listview ColumnClick
Private prevColumnClick As Integer 'to store previous sorted column number
Private secondary_column_to_sort As Integer = 0 'column 0
Private Sub lvLog_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles lvLog.ColumnClick
Dim myListView As ListView = DirectCast(sender, ListView)
Dim sort_order As System.Windows.Forms.SortOrder
If myListView.Columns(e.Column).Tag Is Nothing Then
sort_order = SortOrder.Ascending
Else
' Get previous sort order information from columns .tag
sort_order = DirectCast(myListView.Columns(e.Column).Tag, System.Windows.Forms.SortOrder)
End If
If (prevColumnClick = e.Column) Then
If sort_order = SortOrder.Ascending Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
End If
' Initialize ColumnSorter class
myListView.ListViewItemSorter = New ListViewColumnSorter(e.Column, sort_order, secondary_column_to_sort)
' Perform the sort with these new sort options.
'myListView.Sort()
' Store current column sorting order
myListView.Columns(e.Column).Tag = sort_order
' Store previous column number clicked
prevColumnClick = e.Column
End Sub
source to share
Is it online or winform? On the web, you can compose an expression that has comma separated columns and pass it to the sort () method in the view list
Framework 3.5 and higher though ...
source to share