I'm having trouble getting a quicksort algorithm to work in VBA when trying to sort objects by a property. A bubblesort works fine. I think it has to do with object references, but I can't seem to get it right. Bubblesort used to fail, too, until I added a clone method to my object.
Here is a pastebin of relevant supporting code: http://pastebin.com/egcph0jJ .
This is the relevant function:
Private Sub quick_sort_file_array_by_modified(file_array As Variant, inLowBound As Long, inHighBound As Long)
'Do a quicksort on date_modified
Dim pivot As ParsedFile
Dim tmpSwap As ParsedFile
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLowBound
tmpHi = inHighBound
Set pivot = file_array((inLowBound + inHighBound) / 2)
While (tmpLow <= tmpHi)
Dim tmp_low_file As ParsedFile
Set tmp_low_file = file_array(tmpLow)
While (tmp_low_file.file_mod_date < pivot.file_mod_date And tmpLow < inHighBound)
tmpLow = tmpLow + 1
Wend
Dim tmp_high_file As ParsedFile
Set tmp_high_file = file_array(tmpHi)
While (pivot.file_mod_date < tmp_high_file.file_mod_date And tmpHi > inLowBound)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
Set tmpSwap = file_array(tmpLow)
Set file_array(tmpLow) = file_array(tmpHi).clone
Set file_array(tmpHi) = tmpSwap.clone
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLowBound < tmpHi) Then quick_sort_file_array_by_modified file_array, inLowBound, tmpHi
If (tmpLow < inHighBound) Then quick_sort_file_array_by_modified file_array, tmpLow, inHighBound
End Sub
... the result is it doesn't really change the order of the object array at all.