The example is sorting string arrays, but you can modify it to use any type of arrays, just replace "As String" to the type you need...
VB Code:
Option Explicit Private Sub Form_Load() Dim MyStrArray() As String, K As Long, Q As Long ReDim MyStrArray(1 To 10) Randomize Debug.Print "Unsorted strings:" For K = LBound(MyStrArray) To UBound(MyStrArray) ' create a random string MyStrArray(K) = String(10, " ") For Q = 1 To 10 Mid$(MyStrArray(K), Q, 1) = Chr(Asc("A") + Fix(26 * Rnd)) Next Q ' print the string to the immediate window Debug.Print MyStrArray(K) Next K ' sort the array QuickSort MyStrArray, LBound(MyStrArray), UBound(MyStrArray) ' print the sorted string to the immediate window Debug.Print vbNewLine & "Sorted strings:" For K = LBound(MyStrArray) To UBound(MyStrArray) Debug.Print MyStrArray(K) Next K End Sub Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long) ' ' Made by Michael Ciurescu (CVMichael from vbforums.com) ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url] ' Dim Low As Long, High As Long Dim MidValue As String Low = First High = Last MidValue = C((First + Last) \ 2) Do While C(Low) < MidValue Low = Low + 1 Wend While C(High) > MidValue High = High - 1 Wend If Low <= High Then Swap C(Low), C(High) Low = Low + 1 High = High - 1 End If Loop While Low <= High If First < High Then QuickSort C, First, High If Low < Last Then QuickSort C, Low, Last End Sub Private Sub Swap(ByRef A As String, ByRef B As String) Dim T As String T = A A = B B = T End Sub




Reply With Quote