What entity get ListViewItems sorted by?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RaGa68
    New Member
    • Apr 2021
    • 1

    What entity get ListViewItems sorted by?

    Hello :-)

    I have built a ListView and filled it like this (simplified pseudo code):

    Code:
    Dim clm(3) As String
    
    For i = 0 To rows
    
                    clm(0) = something(i)
                    clm(1) = anything(i)
                    clm(2) = stuff(i)
    
                    Dim itm As ListViewItem = New ListViewItem(clm)
    
                    myListView.Items.Add(itm)
    
    Next

    Now, when the myListView.Sort ing property is set to anything else but "None" the listview will be sorted, ascending or descending. But what determines by which entity it will be sorted?

    The MS docs only say:

    The Sorting property allows you to specify whether or not items are sorted in the ListView control. By default, no sorting is performed. When the Sorting property is set to Ascending or Descending, the items in the ListView are sorted automatically in ascending alphabetical order (when the property is set to Ascending) or descending alphabetical order (when the property is set to Descending).
    1. The "items" are an array each. So by which member of the array will the ListView be sorted?
    2. Can I set a key, and would it make sorting quicker (currently taking a painful 5+ minutes for a 23000 items ListView)?
    3. The ListViewItem.Na me property is often referred to as a 'key', but with the above fill method this property will remain an emtpy string. Does it make any sense to set it to some value?



    Grateful for any insight and help
    - Ralph
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    To have ListView items sorted automatically, set the ListView.Sortin g property to SortOrder.Ascen ding or SortOrder.Desce nding.
    Code:
    myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
    However, with this method, sub-items cannot be compared and sorted, and numbers and times cannot be sorted correctly.
    (Only the first item is sorted.)
    To solve these problems, set the ListView.ListVi ewItemSorter property to an instance of a class that implements the IComparer interface that defines the arrangement.

    Code:
    Public Class ListViewItemComparer
        Implements IComparer
        Private _column As Integer
    	Private _Ascend As Boolean
    	Public Sub New(ByVal col As Integer, ByVal Ascend As Boolean)
    		_column = col
    		_Ascend = Ascend
    	End Sub
    	
    	Public Function CompareTo(ByVal x As Object, ByVal y As Object) _
    		As Integer Implements System.Collections.IComparer.Compare
    		'Get the ListViewItem
    		Dim itemx As ListViewItem = CType(x, ListViewItem)
    		Dim itemy As ListViewItem = CType(y, ListViewItem)
    		
    		If IsNumeric(itemx.SubItems(_column).Text) = True Then
    			Dim xx As Double
    			Dim yy As Double
    			Dim resulta As Boolean = Double.TryParse(itemx.SubItems(_column).Text, xx)
    			Dim resultb As Boolean = Double.TryParse(itemy.SubItems(_column).Text, yy)
    			If _Ascend = True then
    				Return xx.CompareTo(yy)
    			Else
    				Return yy.CompareTo(xx)
    			End If
    		Else
    			If _Ascend = True then
    				Return itemx.SubItems(_column).Text.CompareTo(itemy.SubItems(_column).Text)
    			Else
    				Return itemy.SubItems(_column).Text.CompareTo(itemx.SubItems(_column).Text)
    			End If
    		End If
    	End Function
    End Class
    
    Public Partial Class MainForm
    	Dim AD As Boolean = True
    	Public Sub New()
    		Me.InitializeComponent()
    		myListView.View = View.Details
    		'Set ColumnClick event handler
    		AddHandler myListView.ColumnClick, _
    			AddressOf ListView1_ColumnClick
    		
    		Dim i As Long
    		Dim clm(3) As String
    		Dim rn As New System.Random()
    		Dim rc As New System.Random()
    		For i = 0 To 22999
    			Clm(0) = ChrW(rc.Next(AscW("A"), AscW("Z") + 1)) & ChrW(rc.Next(AscW("A"), AscW("Z") + 1)) & ChrW(rc.Next(AscW("A"), AscW("Z") + 1))
    			clm(1) = rn.Next().ToString
    			clm(2) = i.ToString
    			Dim itm As ListViewItem = New ListViewItem(clm)
    			myListView.Items.Add(itm)
    		Next i
    		
    		'Add columns
    		myListView.Columns.Add("String", 100, HorizontalAlignment.Left)
    		myListView.Columns.Add("Numeric1", 100, HorizontalAlignment.Right)
    		myListView.Columns.Add("Numeric2", 100, HorizontalAlignment.Right)
    		'Sort Ascending
    		myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
    	End Sub
    	
    	Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
    		myListView.ListViewItemSorter = New ListViewItemComparer(e.Column, AD)
    		If AD = True Then
    			AD = False
    		Else
    			AD = True
    		End If
    	End Sub
    End Class
    The code above sorts the items in ascending / descending order by clicking on the column header.

    Comment

    • madankarmukta
      Contributor
      • Apr 2008
      • 308

      #3
      Please restrict the sort order to some value.It also constrained to datatype.

      Let me know the datatype please.

      Comment

      Working...