clsmaster
asked on
Sorting function
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.
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.
ASKER
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.
ASKER
Actually I want to change this a little. Instead of returning the array I want it to order the two arrays.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..
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..
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 :-)
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
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
The last code won't make sense (or work) if arrTotals and arrValues are different sizes.
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
ASKER
Everything looks good Thanks
For I = 1 to n
List1.AddItem arrValues(i)
List1.ItemData(List1.NewIn
Next I
The list items will contain the strings, and the itemdata will contain the numbers