Public Function QuickSort(coll As Collection, Optional l As Long = 1, Optional r As Long = -1) As Collection
' sort the collection using QuickSort algorithm.
' This is a more efficient sorting algorithm than BubbleSort
'
' Args:
' coll: Unsorted Collection.
' l: The left boundary of the range to be sorted (default is 1)
' r: The right boundary of the range to be sorted (default is -1, which means till the end of the collection)
'
' Returns:
' Sorted Collection
If r = -1 Then r = coll.Count
If l < r Then
Dim pivotIndex As Long
pivotIndex = QuickSortPartition(coll, l, r)
QuickSort coll, l, pivotIndex - 1
QuickSort coll, pivotIndex + 1, r
End If
Set QuickSort = coll
End Function
Private Function QuickSortPartition(coll As Collection, l As Long, r As Long) As Long
' partitions the collection into two parts based on the pivot element
'
' Args:
' coll: The Collection object to be partitioned
' l: The left boundary of the range to be partitioned
' r: The right boundary of the range to be partitioned
'
' Returns:
' pivot index
Dim pivot As Variant, i As Long, j As Long
pivot = coll(r)
i = l - 1
For j = l To r - 1
If coll(j) <= pivot Then
i = i + 1
' swap coll(i) with coll(j)
assign coll(i), coll(j)
End If
Next j
' swap coll(i + 1) with coll(r)
assign coll(i + 1), coll(r)
QuickSortPartition = i + 1
End Function
Maybe take a look if this is a good sorting option (add as an issue)