468,114 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

What entity get ListViewItems sorted by?

1
Hello :-)

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

Expand|Select|Wrap|Line Numbers
  1. Dim clm(3) As String
  2.  
  3. For i = 0 To rows
  4.  
  5.                 clm(0) = something(i)
  6.                 clm(1) = anything(i)
  7.                 clm(2) = stuff(i)
  8.  
  9.                 Dim itm As ListViewItem = New ListViewItem(clm)
  10.  
  11.                 myListView.Items.Add(itm)
  12.  
  13. Next
  14.  

Now, when the myListView.Sorting 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.Name 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
6 Days Ago #1
2 1134
SioSio
238 128KB
To have ListView items sorted automatically, set the ListView.Sorting property to SortOrder.Ascending or SortOrder.Descending.
Expand|Select|Wrap|Line Numbers
  1. 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.ListViewItemSorter property to an instance of a class that implements the IComparer interface that defines the arrangement.

Expand|Select|Wrap|Line Numbers
  1. Public Class ListViewItemComparer
  2.     Implements IComparer
  3.     Private _column As Integer
  4.     Private _Ascend As Boolean
  5.     Public Sub New(ByVal col As Integer, ByVal Ascend As Boolean)
  6.         _column = col
  7.         _Ascend = Ascend
  8.     End Sub
  9.  
  10.     Public Function CompareTo(ByVal x As Object, ByVal y As Object) _
  11.         As Integer Implements System.Collections.IComparer.Compare
  12.         'Get the ListViewItem
  13.         Dim itemx As ListViewItem = CType(x, ListViewItem)
  14.         Dim itemy As ListViewItem = CType(y, ListViewItem)
  15.  
  16.         If IsNumeric(itemx.SubItems(_column).Text) = True Then
  17.             Dim xx As Double
  18.             Dim yy As Double
  19.             Dim resulta As Boolean = Double.TryParse(itemx.SubItems(_column).Text, xx)
  20.             Dim resultb As Boolean = Double.TryParse(itemy.SubItems(_column).Text, yy)
  21.             If _Ascend = True then
  22.                 Return xx.CompareTo(yy)
  23.             Else
  24.                 Return yy.CompareTo(xx)
  25.             End If
  26.         Else
  27.             If _Ascend = True then
  28.                 Return itemx.SubItems(_column).Text.CompareTo(itemy.SubItems(_column).Text)
  29.             Else
  30.                 Return itemy.SubItems(_column).Text.CompareTo(itemx.SubItems(_column).Text)
  31.             End If
  32.         End If
  33.     End Function
  34. End Class
  35.  
  36. Public Partial Class MainForm
  37.     Dim AD As Boolean = True
  38.     Public Sub New()
  39.         Me.InitializeComponent()
  40.         myListView.View = View.Details
  41.         'Set ColumnClick event handler
  42.         AddHandler myListView.ColumnClick, _
  43.             AddressOf ListView1_ColumnClick
  44.  
  45.         Dim i As Long
  46.         Dim clm(3) As String
  47.         Dim rn As New System.Random()
  48.         Dim rc As New System.Random()
  49.         For i = 0 To 22999
  50.             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))
  51.             clm(1) = rn.Next().ToString
  52.             clm(2) = i.ToString
  53.             Dim itm As ListViewItem = New ListViewItem(clm)
  54.             myListView.Items.Add(itm)
  55.         Next i
  56.  
  57.         'Add columns
  58.         myListView.Columns.Add("String", 100, HorizontalAlignment.Left)
  59.         myListView.Columns.Add("Numeric1", 100, HorizontalAlignment.Right)
  60.         myListView.Columns.Add("Numeric2", 100, HorizontalAlignment.Right)
  61.         'Sort Ascending
  62.         myListView.Sorting = System.Windows.Forms.SortOrder.Ascending
  63.     End Sub
  64.  
  65.     Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
  66.         myListView.ListViewItemSorter = New ListViewItemComparer(e.Column, AD)
  67.         If AD = True Then
  68.             AD = False
  69.         Else
  70.             AD = True
  71.         End If
  72.     End Sub
  73. End Class
  74.  
The code above sorts the items in ascending / descending order by clicking on the column header.
5 Days Ago #2
madankarmukta
306 256MB
Please restrict the sort order to some value.It also constrained to datatype.

Let me know the datatype please.
2 Days Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Douglas Reith | last post: by
1 post views Thread by Razvan | last post: by
11 posts views Thread by Arsen Vladimirskiy | last post: by
3 posts views Thread by peorro77 | last post: by
8 posts views Thread by Chad Miller | last post: by
1 post views Thread by =?Utf-8?B?VmljdG9y?= | last post: by
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.