Solved

sorting....

Posted on 1998-10-07
2
229 Views
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?

 Thanks!
  Craig


=================================================
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
            Do While varArray(i, 1) < varTestVal
                i = i + 1
            Loop
            Do While varArray(j, 1) > varTestVal
                j = j - 1
            Loop
            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)
        Else
            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




0
Comment
Question by:cyberimage
2 Comments
 
LVL 2

Expert Comment

by:schild
Comment Utility
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
Schild


0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 150 total points
Comment Utility
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
with
   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
else
   if (x1(1) = x2(1)) then
        if (x1(2) > x2(2)) then
            IsLessThen = true
        else
            IsLessThen = false
        end if
   else
      IsLessThen = false
   end if
end if
end function
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now