
Originally Posted by
JimAvanti
Is there any reason you are against sorting in-place? does it take longer to sort that way?
I haven't tested this scenario specifically but calling Add repeatedly to add multiple items to a ListView gets exponentially slower because the control redraws after each Add call. I'd expect the same to happen at least once - possibly twice - when you call Remove and Insert. If you really wanted to swap data between items then I'd suggest writing a method or two to do it. You should already have the items themselves because you would have to have compared them to determine that they need to be swapped, so you shouldn't need to work with indexes:
vb.net Code:
Private Sub SwapListViewItems(item1 As ListViewItem, item2 As ListViewItem)
Dim text As String
For i = 0 To item1.SubItems.Count - 1
Dim subitem1 = item1.SubItems(i)
Dim subitem2 = item2.SubItems(i)
text = subitem1.Text
subitem1.Text = subitem2.Text
subitem2.Text = text
Next
End Sub
If you must use indexes:
vb.net Code:
Private Sub SwapListViewItems(index1 As Integer)
Dim item1 = ListView1.Items(index1)
Dim item2 = ListView1.Items(index1 + 1)
Dim text As String
For i = 0 To item1.SubItems.Count - 1
Dim subitem1 = item1.SubItems(i)
Dim subitem2 = item2.SubItems(i)
text = subitem1.Text
subitem1.Text = subitem2.Text
subitem2.Text = text
Next
End Sub