Multidimensional QuickSort on 1 Dimension:   Return to List

' Sample array
aryTest(1,1) = "Jones, Jim"' 1st dimension of 1st record
aryTest(2,1) = "Virginia"' 2nd dimension of 1st record
aryTest(3,1) = "25"' 3rd dimension of 1st record
aryTest(1,2) = "Smith, Mary"' 1st dimension of 2nd record
aryTest(2,2) = "North Carolina"' 2nd dimension of 2nd record
aryTest(3,2) = "36"' 3rd dimension of 2nd record
aryTest(1,3) = "Johnson, Steve"' 1st dimension of 3rd record
aryTest(2,3) = "Mississippi"' 2nd dimension of 3rd record
aryTest(3,3) = "45"' 3rd dimension of 3rd record

' Command which calls quicksort routine below and will sort the array on 3rd dimension
MyQuickSort_Single aryTest(), 1, UBound(aryTest(),2), 3, True

' The called quicksort subroutine (array passed By Reference -- so array itself is modified)
Private Sub MyQuickSort_Single(ByRef SortArray As Variant, _
            ByVal First As Long, ByVal Last As Long, _
            ByVal PrimeSort As Integer, ByVal Ascending as Boolean)
Dim Low As Long, High As Long
Dim Temp As Variant, List_Separator As Variant
Dim TempArray() As Variant
ReDim TempArray(UBound(SortArray, 1))
Low = First
High = Last
List_Separator1 = SortArray(PrimeSort, (First + Last) / 2)
Do
    If Ascending = True Then
        Do While (SortArray(PrimeSort, Low) < List_Separator1)
            Low = Low + 1
        Loop
        Do While (SortArray(PrimeSort, High) > List_Separator1)
            High = High - 1
        Loop
    Else
        Do While (SortArray(PrimeSort, Low) > List_Separator1)
            Low = Low + 1
        Loop
        Do While (SortArray(PrimeSort, High) < List_Separator1)
            High = High - 1
        Loop
    End If
    If (Low <= High) Then
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            TempArray(i) = SortArray(i, Low)
        Next
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            SortArray(i, Low) = SortArray(i, High)
        Next
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            SortArray(i, High) = TempArray(i)
        Next
        Low = Low + 1
        High = High - 1
    End If
Loop While (Low <= High)
If (First < High) Then MyQuickSort_Single SortArray, First, High, PrimeSort, Ascending
If (Low < Last) Then MyQuickSort_Single SortArray, Low, Last, PrimeSort, Ascending
End Sub



Note to Webmaster