• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:


The following is a sort routine that I got from a book.

An array of times (1 to 10,3)
where times(x,1) is a time in mil. secs.
where times(x,2) is an ID
where times(x,3) a number from 0 to ??

I need to sort first on the time from least to greater
then on the number from greatest to least.

By looking at this routine can somebody help me with this?


Option Compare Database
Option Explicit

' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.

' Quicksort for simple data types.

' Indicate that a parameter is missing.
Const dhcMissing = -2

Sub dhQuickSort(varArray As Variant, _
 Optional intLeft As Integer = dhcMissing, _
 Optional intRight As Integer = dhcMissing)

    ' From "VBA Developer's Handbook"
    ' by Ken Getz and Mike Gilbert
    ' Copyright 1997; Sybex, Inc. All rights reserved.
    ' Entry point for sorting the array.
    ' This technique uses the recursive Quicksort
    ' algorithm to perform its sort.
    ' In:
    '   varArray:
    '       A variant pointing to an array to be sorted.
    '       This had better actually be an array, or the
    '       code will fail, miserably. You could add
    '       a test for this:
    '       If Not IsArray(varArray) Then Exit Sub
    '       but hey, that would slow this down, and it's
    '       only YOU calling this procedure.
    '       Make sure it's an array. It's your problem.
    '   intLeft:
    '   intRight:
    '       Lower and upper bounds of the array to be sorted.
    '       If you don't supply these values (and normally, you won't)
    '       the code uses the LBound and UBound functions
    '       to get the information. In recursive calls
    '       to the sort, the caller will pass this information in.
    '       To allow for passing integers around (instead of
    '       larger, slower variants), the code uses -2 to indicate
    '       that you've not passed a value. This means that you won't
    '       be able to use this mechanism to sort arrays with negative
    '       indexes, unless you modify this code.
    ' Out:
    '       The data in varArray will be sorted.
    Dim i As Integer
    Dim j As Integer
    Dim varTestVal As Variant
    Dim intMid As Integer

    If intLeft = dhcMissing Then intLeft = LBound(varArray)
    If intRight = dhcMissing Then intRight = UBound(varArray)
    If intLeft < intRight Then
        intMid = (intLeft + intRight) \ 2
        varTestVal = varArray(intMid, 1)
        i = intLeft
        j = intRight
            Do While varArray(i, 1) < varTestVal
                i = i + 1
            Do While varArray(j, 1) > varTestVal
                j = j - 1
            If i <= j Then
                SwapElements varArray, i, j
                i = i + 1
                j = j - 1
            End If
        Loop Until i > j
        ' To optimize the sort, always sort the
        ' smallest segment first.
        If j <= intMid Then
            Call dhQuickSort(varArray, intLeft, j)
            Call dhQuickSort(varArray, i, intRight)
            Call dhQuickSort(varArray, i, intRight)
            Call dhQuickSort(varArray, intLeft, j)
        End If
    End If
End Sub

Private Sub SwapElements(varItems As Variant, intItem1 As Integer, intItem2 As Integer)
    Dim varTemp As Variant
    Dim varTemp2 As Variant
    Dim varTemp3 As Variant
    varTemp = varItems(intItem2, 1)
    varTemp2 = times(intItem2, 2)
    varTemp3 = times(intItem2, 3)
    varItems(intItem2, 1) = varItems(intItem1, 1)
    times(intItem2, 2) = times(intItem1, 2)
    times(intItem2, 3) = times(intItem1, 3)
    varItems(intItem1, 1) = varTemp
    times(intItem1, 2) = varTemp2
    times(intItem1, 3) = varTemp3
End Sub

1 Solution
What is your question?
Are you asking how to use the qsort subroutine?
What do you means by writing:
"I need to sort first on the time from least to greater then on the number from greatest to least."
Do you mean you need to that the number is sub sort of the time? Which means if the time equal you sort by the number. Or may be you wanted to make two separated qsorts, one by time the second by number?

If you are asking how to use the sort subrutine

If you are asking about main sorting by time sun-sorting by number, this sorting function will not make the whole work for you, you will have to first sort by time, the take an equal series of equal time and sort them by the number. If you don’t know how to make the sub-sorting, pleasse let me know.

Good Luck

Restructure you variant array to one array of variants, so that you can do the following statements
t1 = times(1)
t1(1) is a time in mil. secs.
t2(2) is an ID
t3(3) a number from 0 to ??

replace (twice)
   varArray(i, 1) < varTestVal
   IsLessThen (varArray(i, 1) , varTestVal)

and add
private function IsLessThen(byval x1 as variant, byval x2 as variant)
'add your compare code here
if x1(1) < x2(1) then
    IsLessThen = true
   if (x1(1) = x2(1)) then
        if (x1(2) > x2(2)) then
            IsLessThen = true
            IsLessThen = false
        end if
      IsLessThen = false
   end if
end if
end function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now