Solved

Sorting function

Posted on 1998-07-02
10
133 Views
Last Modified: 2010-05-03
I have two arrays that I want to put in order from the greatest to the least.  I want to do this through a function that returns another array back.
Here is the description of the function that I want to use

Private Function PutInOrder(arrTotals() As Integer, arrValues() As String, HowMany as Integer) As Variant

I want it order by the array arrTotals and the array that is return I want made up of the corressponding values from arrvalue.  The indexes are the same, ie arrTotals(3) is the totals for arrValue(3) <- and this is what should be put into the final array.  The HowMany is how large I want the array.  So it will return say only the top 8 values.

Now a few requirements speed is not the overpowering issue, although somewhat important, the fewest possible resource is more important.
0
Comment
Question by:clsmaster
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1464846
If the speed is not the main issue, and you have not more then 1000 items to sortt, you can simply add them to a sorted listbox:
For I = 1 to n
List1.AddItem arrValues(i)
List1.ItemData(List1.NewIndex)=arrTotals(i)
Next I

The list items will contain the strings, and the itemdata will contain the numbers
0
 

Author Comment

by:clsmaster
ID: 1464847
Speed is not important, but like I said resource are I don't want to have to create a form and a listbox and all just to attain what I'm looking for.  What I want returned is the array, so that it can be further processed.
0
 

Author Comment

by:clsmaster
ID: 1464848
Actually I want to change this a little.  Instead of returning the array I want it to order the two arrays.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 6

Accepted Solution

by:
clifABB earned 50 total points
ID: 1464849
Private Function PutInOrder(arrTotals() As Integer, arrValues() As String, HowMany as Integer) As Variant

Dim nTemp As Integer
Dim nCnt1 As Integer
Dim nCnt2 As Integer

For nCnt1 = 1 to UBound(arrTotals) - 1
  For nCnt2 = nCnt1 + 1 to UBound(arrTotals)
    If arrTotals(nCnt1) > arrTotals(nCnt2) Then
      nTemp = arrTotals(nCnt1)
      arrTotals(nCnt1) = arrTotals(nCnt2)
      arrTotals(nCnt2) = nTemp
    End If
  Next nCnt2
Next nCnt1

For nCnt1 = 1 to UBound(arrValues) - 1
  For nCnt2 = nCnt1 + 1 to UBound(arrValues)
    If arrValues(nCnt1) > arrValues(nCnt2) Then
      nTemp = arrValues(nCnt1)
      arrValues(nCnt1) = arrValues(nCnt2)
      arrValues(nCnt2) = nTemp
    End If
  Next nCnt2
Next nCnt1
End Function
0
 
LVL 6

Expert Comment

by:alamo
ID: 1464850
So are you saying that instead of just the top HowMany entries you want the entire arrays sorted?

Would it be acceptable to return an integer array giving the sorted indices of the original arrays? That would probably be easier and quicker, and you wouldn't have to make a duplicate of the original arrays for sorting..
0
 
LVL 6

Expert Comment

by:alamo
ID: 1464851
clif, I think the two arrays need to be ordered in step, i.e. both sorted based on the values in arrTotals (unless the question has changed unrecognizably from the original :-)
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1464852
In that case, replace the two loops with this one:
For nCnt1 = 1 to UBound(arrTotals) - 1
  For nCnt2 = nCnt1 + 1 to UBound(arrTotals)
    If arrTotals(nCnt1) > arrTotals(nCnt2) Then
      nTemp = arrTotals(nCnt1)
      arrTotals(nCnt1) = arrTotals(nCnt2)
      arrTotals(nCnt2) = nTemp
      nTemp = arrValues(nCnt1)
      arrValues(nCnt1) = arrValues(nCnt2)
      arrValues(nCnt2) = nTemp
    End If
  Next nCnt2
Next nCnt1

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1464853
The last code won't make sense (or work) if arrTotals and arrValues are different sizes.
0
 
LVL 2

Expert Comment

by:Jagar
ID: 1464854
Sorry for the inactivity on this question, but I've been away for the weekend.  I'll get to checking this sometime today and then grade your answer
0
 

Author Comment

by:clsmaster
ID: 1464855
Everything looks good Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 44
Macro Excel - Multiple If conditions 2 81
Automatic Email Reminder 4 56
Export PDF Form fields to Access  or Excel  in Tab order 16 77
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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

790 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