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




Reply With Quote