Hi Redhawk
This isn't exactly what you want but it does the job very well. The
algorithm is, reputedly, the fastest there is as it moves data through
greater steps than bubble sorts - it 'best guesses where en element should be
rather than repeatedly moving it, then moves elements in chunks. The coding
is 'borrowed' from the VBA Developer's Handbook - my bible!
Pass the array as the varArray element. the coding will return the array
sorted. You can specify the left and right limits of sort if required
(lngLeft, lngRight)
Private Sub dhQuickSort(varArray As Variant, Optional lngLeft As Long =
dhcMissing, Optional lngRight As Long = dhcMissing)
Dim i As Long, j As Long, VarTestVal As Variant, lngMid As Long
If lngLeft = dhcMissing Then lngLeft = LBound(varArray)
If lngRight = dhcMissing Then lngRight = UBound(varArray)
If lngLeft < lngRight Then
lngMid = (lngLeft + lngRight) \ 2
VarTestVal = varArray(lngMid)
i = lngLeft
j = lngRight
Do
Do While varArray(i) < VarTestVal
i = i + 1
Loop
Do While varArray(j) > VarTestVal
j = j - 1
Loop
If i <= j Then
SwapElements varArray, i, j
i = i + 1
j = j - 1
End If
Loop Until i > j
If j <= lngMid Then
Call dhQuickSort(varArray, lngLeft, j)
Call dhQuickSort(varArray, i, lngRight)
Else
Call dhQuickSort(varArray, i, lngRight)
Call dhQuickSort(varArray, lngLeft, j)
End If
End If
End Sub
Private Sub SwapElements(varItems As Variant, lngItem1 As Long, lngItem2 As
Long)
Dim varTemp As Variant
varTemp = varItems(lngItem2)
varItems(lngItem2) = varItems(lngItem1)
varItems(lngItem1) = varTemp
End Sub