So I'm trying to write a QuickSort for a ListView control. Built in ListView sorting is only alphabetical, not numeric, thus the need for this.
But I'm getting errors left and right. Mainly the problem is in moving the values around. It seems to be handling the rest of the quicksort logic without issue.
Typically I would just
storeItem = leftItem
leftItem = rightItem
rightItem = storeItem
however, the reassignment of the values of the listsubitems is being a reeeaaal pita. Any advice, or anyone know the 'correct' methods I should be calling on these? I've been at this for hours and can't find aaanything.
Code:
Public Sub SortByDate(ByRef lvwBox As Object, dateColumn As Integer, left As Integer, right As Integer)
Dim rightIndex As Integer
Dim leftIndex As Integer
Dim comparisonItem As Variant
Dim storedItem As ListItem
leftIndex = left
rightIndex = right
comparisonItem = lvwBox.ListItems(CInt((leftIndex + rightIndex) / 2)).SubItems(dateColumn)
Do
Do While (lvwBox.ListItems(leftIndex).SubItems(dateColumn) < comparisonItem And leftIndex < rightIndex)
leftIndex = leftIndex + 1
Loop
Do While (comparisonItem < lvwBox.ListItems(rightIndex).SubItems(dateColumn) And rightIndex > leftIndex)
rightIndex = rightIndex - 1
Loop
If leftIndex <= rightIndex Then
'This is the part giving errors.
Dim i As Integer
Set storedItem = lvwBox.ListItems(leftIndex)
For i = 1 To lvwBox.ListItems(leftIndex).ListSubItems.Count
storedItem.ListSubItems.Add i, , lvwBox.ListItems(leftIndex).SubItems(i)
Next
lvwBox.ListItems.Remove leftIndex
lvwBox.ListItems.Add (leftIndex)
lvwBox.ListItems(leftIndex) = lvwBox.ListItems(rightIndex)
For i = 1 To lvwBox.ListItems(rightIndex).ListSubItems.Count
lvwBox.ListItems(leftIndex).ListSubItems.Add i, , lvwBox.ListItems(rightIndex).SubItems(i)
Next
lvwBox.ListItems(rightIndex) = storedItem
For i = 1 To lvwBox.ListItems(leftIndex).ListSubItems.Count
lvwBox.ListItems(rightIndex).ListSubItems.Add i, , storedItem.ListSubItems.Item(i).text
Next
leftIndex = leftIndex + 1
rightIndex = rightIndex - 1
End If
Loop While leftIndex <= rightIndex
If left < rightIndex Then
SortByDate lvwBox, dateColumn, left, rightIndex
End If
If leftIndex < right Then
SortByDate lvwBox, dateColumn, leftIndex, right
End If
End Sub