Posted on 1998-10-07
Last Modified: 2008-03-17
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

Question by:cyberimage

Expert Comment

ID: 1438781
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

LVL 13

Accepted Solution

Mirkwood earned 150 total points
ID: 1438782
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question