QuickSort is a pretty standard and elegant sorting algorithm, you can find reference to it in all decent numerical methods books and pretty easily on Google.
Since its so useful I thought I'd post an example of the QuickSort algorithm being implemented in VBA so that you can dump the code into a separate module and not worry too much as to what its doing. NB I only just knocked this together. I've tested this out with a bunch of random numbers and its appears to work ok but give me a shout if its not working properly and I'll have a look into it.
Maccas
Code:
Option Explicit
Public Sub QuickSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)
Dim m As Long
' Sort array A(lb..ub)
Do While Lb < Ub
' Quickly sort short lists
If (Ub - Lb <= 12) Then
Call InsertSort(A, Lb, Ub)
Exit Sub
End If
' Partition into two segments
m = Partition(A, Lb, Ub)
' Sort the smallest partition to minimize stack requirements
If m - Lb <= Ub - m Then
Call QuickSort(A, Lb, m - 1)
Lb = m + 1
Else
Call QuickSort(A, m + 1, Ub)
Ub = m - 1
End If
Loop
End Sub
Private Function Partition(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long) As Long
Dim t As Variant
Dim pivot As Variant
Dim i As Long
Dim j As Long
Dim p As Long
' Partition array[lb..ub]
' Select pivot and exchange with 1st element
p = Lb + (Ub - Lb) \ 2
pivot = A(p)
A(p) = A(Lb)
' Sort Lb+1 .. Ub based on pivot
i = Lb
j = Ub + 1
Do
Do
j = j - 1
Loop While j > i And A(j) > pivot
Do
i = i + 1
Loop While i < j And A(i) < pivot
' Only way out of do loop
If i >= j Then Exit Do
' Swap A(i), A(j)
t = A(i)
A(i) = A(j)
A(j) = t
Loop While True
' Pivot belongs in A(j)
A(Lb) = A(j)
A(j) = pivot
Partition = j
End Function
Private Sub InsertSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)
Dim t As Variant
Dim i As Long
Dim j As Long
' Sort A[Lb..Ub]
For i = Lb + 1 To Ub
t = A(i)
' Shift elements down until insertion point found
For j = i - 1 To Lb Step -1
If A(j) <= t Then Exit For
A(j + 1) = A(j)
Next j
' Insert
A(j + 1) = t
Next i
End Sub