|
-
Nov 1st, 2006, 02:01 PM
#1
Thread Starter
Frenzied Member
FYI: Sort ListView - Another Way of Doing It
I have seen many posts where someone wanted to sort a ListView by a column other than the first. MSDN and some of the more seasoned developers in this forum lead you down the IComparer route, which is probably the best practice. However, not all situations warrant the sometimes lengthy code of the "best practice". The code below is a method I use for sorting ListViews in smaller applications. If you remove my comments then you can see that sorting can be achieved in as little as 20-30 lines of code.
Code:
' I created this structure to represent one item in the ListView.
' You do not have to create a structure, you can simply use individual variables.
' My ListView has 4 columns: Component, UsedOn, Production, and Rank.
Public Structure ItemInList
Dim Component As String
Dim UsedOn As String
Dim Production As String
Dim Rank As String
End Structure
' Here I have created a sub for the sorting procedure. You can call this sub
' from the ColumnHeader.Click() event in the ListView.
Private Sub SortListViewByRankColumn()
Dim relocate As ItemInList 'This will be the current item we are looking at.
Dim itemsMoved As Boolean 'This will tell us if we made any changes to order.
Dim index As Integer 'This is the index of the current ListView item.
' The outside Do loop makes sure we keep moving things around until we
' arrive at the order we want.
Do
' itemsMoved is a boolean that we set to True if we make any changes to the
' order of the ListView.
itemsMoved = False
' The inside For loop is the main gear. The column in my ListView that I am
' sorting by contains numbers. So I can choose whether I want to sort in
' Ascending or Descending order, or I can let the user choose. If your column
' does not contain letters, then you can simply use the ASCII values of the
' first letter, and then refine your order by sorting by the second and then
' third letter for all items that start with the same first letter.
For index = 0 To (Me.ListView1.Items.Count - 2)
' The number in the fourth column of my ListView is formatted to two
' decimal places. So here I compare the first item to the second item.
' I am sorting in Descending order, so if the first item is less than
' the second item, then I move it to the bottom of the list.
' If you want to sort in Ascending order, then change this to see if
' the first item is greater than the second item.
If Double.Parse(Me.ListView1.Items.Item(index).SubItems(3).Text) < _
Double.Parse(Me.ListView1.Items.Item(index + 1).SubItems(3).Text) Then
' If the item is lower than the item below it, then save it's values
' into the relocate variable.
relocate.Component = Me.ListView1.Items.Item(index).SubItems(0).Text()
relocate.UsedOn = Me.ListView1.Items.Item(index).SubItems(1).Text()
relocate.Production = Me.ListView1.Items.Item(index).SubItems(2).Text()
relocate.Rank = Me.ListView1.Items.Item(index).SubItems(3).Text()
' Now remove that item from the list. This moves the rest of the items
' in the list up one.
Me.ListView1.Items.Item(index).Remove()
' Now create a new ListViewItem, it will be added as the last item
' in the list; which is where we want it.
Dim newItem As New ListViewItem
' Use the values that you saved from the item you deleted to add the
' new item.
newItem = Me.ListView1.Items.Add(relocate.Component)
newItem.SubItems.Add(relocate.UsedOn)
newItem.SubItems.Add(relocate.Production)
newItem.SubItems.Add(relocate.Rank)
' Make sure you set your itemsMoved variable to True. This makes sure
' we keep looping through this process until no more changes need to
' be made.
itemsMoved = True
End If
Next index
' When we have made no changes throughout the inner loop, we are done.
Loop Until itemsMoved = False
End Sub
-
Nov 1st, 2006, 03:19 PM
#2
Re: FYI: Sort ListView - Another Way of Doing It
no disrespect to your code, but making a comparer for use to sort a listview is not lengthy code... probably about 20-30 lines
-
Nov 1st, 2006, 03:23 PM
#3
Thread Starter
Frenzied Member
Re: FYI: Sort ListView - Another Way of Doing It
I agree. I noted that it is sometimes lengthy depending on the specific situation. My method is targeted more towards beginners who are not quite ready to take the IComparer route.
-
Nov 1st, 2006, 06:44 PM
#4
Re: FYI: Sort ListView - Another Way of Doing It
Creating an IComparer for sorting a ListView is only lengthy if you have many columns containing data of different types, thus each column must be compared in a different way. Using a method other than an IComparer is going to become lengthy in those situations too. I don't doubt that your code does what it's supposed to but I don't think it's necessarily any quicker to code than creating an IComparer, or easier if you grasp the concept of implementing an interface. Creating your own IComparer is also a good chance to learn some OO principles if you don't already know how to implement an interface.
You should also ask a Mod to move this thread to the VB.NET CodeBank.
-
Nov 2nd, 2006, 01:28 AM
#5
Thread Starter
Frenzied Member
Re: FYI: Sort ListView - Another Way of Doing It
-
Nov 2nd, 2006, 01:34 AM
#6
Re: FYI: Sort ListView - Another Way of Doing It
Take a look down the list of forums on the front page of the site.
-
Nov 2nd, 2006, 02:18 AM
#7
Re: FYI: Sort ListView - Another Way of Doing It
Here is the shortest you can get.
VB Code:
' Implements the manual sorting of items by index.
Friend Class ListViewItemComparer
Implements IComparer
Private ind As Integer
Public Sub New()
ind = 0
End Sub
Public Sub New(ByVal index As Integer)
ind = index
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).Index.ToString, CType(y, ListViewItem).Index.ToString)
End Function
End Class
Usage based on the first item but can be changed to a selected item or column etc.:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lvwItm As ListViewItem
lvwItm = DirectCast(Me.ListView1.Items(1).Clone, ListViewItem)
Me.ListView1.ListViewItemSorter = New ListViewItemComparer(lvwItm.Index)
lvwItm = Nothing
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 2nd, 2006, 02:21 AM
#8
Re: FYI: Sort ListView - Another Way of Doing It
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 2nd, 2006, 08:28 AM
#9
Thread Starter
Frenzied Member
Re: FYI: Sort ListView - Another Way of Doing It
So is it possible to add my code to the CodeBank? It's targeted more towards beginners who are not quite ready to learn the IComparer class.
-
Nov 2nd, 2006, 09:35 AM
#10
Re: FYI: Sort ListView - Another Way of Doing It
 Originally Posted by RobDog888
yeah that works pretty well. I need to clean that up a bit one day, I could probably make it more efficient.
Ok. Moved to codebank.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|