Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sorting....

Posted on 1998-10-07
2
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

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


0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 300 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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

730 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