'These are all Quicksort routines. Quicksort is usually the fastest option 'They allow you to sort on specific columns of multi-dimensional arrays without having to touch the array itself. '=========================================================== 'This is your vanilla sort routine 'It takes an array of Longs (change the first two lines if you want something else) 'and returns a sorted array. So it does rearrange the items 'Usage: 'Sort SomeArray() 'SomeArray(1) will hold the lowest value (assuming the array starts at 1) '=========================================================== Public Sub Sort(SortList() As Long) Dim switches&, gap&, hold As Long, ii&, Low&, Num& Low = LBound(SortList) Num = UBound(SortList) gap = Num - Low + 1 Do gap = Int((gap / 13) * 10) If gap < 1 Then gap = 1 If gap = 9 Or gap = 10 Then gap = 11 switches = 0 For ii = Low To Num - gap If SortList(ii) > SortList(ii + gap) Then hold = SortList(ii) SortList(ii) = SortList(ii + gap) SortList(ii + gap) = hold switches = 1 End If Next ii Loop Until switches + gap = 1 End Sub '=========================================================== 'This is a linked list sort routine 'It takes an array of Longs (change the first two lines if you want something else) 'as well as an array to put the sort order into, and returns a sorted array. 'It does NOT rearrange the items in the original array 'Usage: 'Dim sList() As Long 'this should always be Long, because it contains a sequence 'SortLinked SomeArray(), sList() 'SomeArray(sList(1)) will hold the lowest value (assuming SomeArray starts at 1) 'SomeArray(sList(10)) will hold the 10th lowest value 'SomeArray(1) will still hold what it did before the sort '=========================================================== Public Sub SortLinked(SortList() As Long, sList() As Long) Dim switches&, gap&, hold As Long, ii&, Low&, Num& Low = 1 Num = UBound(SortList) ReDim sList(Num) Dim t As Single t = Timer For ii = 1 To Num sList(ii) = ii Next ii gap = Num - Low + 1 Do gap = Int((gap / 13) * 10) If gap < 1 Then gap = 1 If gap = 9 Or gap = 10 Then gap = 11 switches = 0 For ii = Low To Num - gap If SortList(sList(ii)) > SortList(sList(ii + gap)) Then hold = sList(ii) sList(ii) = sList(ii + gap) sList(ii + gap) = hold switches = 1 End If Next ii Loop Until switches + gap = 1 End Sub '=========================================================== 'This is a linked list sort routine 'It takes '* an array of Longs (change the first two lines if you want something else) '* an array to put the sort order into, and '* the column to sort on, and 'it returns a sorted array. 'It does NOT rearrange the items in the original array 'Usage: 'Dim sList() As Long 'this should always be Long, because it contains a sequence 'SortLinked SomeArray(), sList(), 3 'SomeArray(sList(1),3) will hold the lowest value in column 3 (assuming SomeArray starts at 1) 'SomeArray(sList(10),3) will hold the 10th lowest value in column 3 'SomeArray(1,3) will still hold what it did before the sort '=========================================================== Public Sub SortLinkedCol(SortList() As Single, sList() As Long, tCol As Long) Dim switches&, gap&, hold As Long, ii&, Low&, Num& Low = 1 Num = UBound(SortList) ReDim sList(Num) Dim t As Single t = Timer For ii = 1 To Num sList(ii) = ii Next ii gap = Num - Low + 1 Do gap = Int((gap / 13) * 10) If gap < 1 Then gap = 1 If gap = 9 Or gap = 10 Then gap = 11 switches = 0 For ii = Low To Num - gap If SortList(sList(ii), tCol) > SortList(sList(ii + gap), tCol) Then hold = sList(ii) sList(ii) = sList(ii + gap) sList(ii + gap) = hold switches = 1 End If Next ii Loop Until switches + gap = 1 End Sub