Solved

simple two-dimensional array sort

Posted on 2003-11-11
7
3,251 Views
Last Modified: 2010-05-01
This is giving me alot of trouble.  I have an array with n elements.  Each element has a NAME and a SCORE
I need to sort the array descending by score... so the student with the highest score is at the top.  50 is the max scores the class may have,but there maybe less, which is where n - 1 comes in as the number of scores actually found for a particular class.  This is what I have to sort:

Dim rankArray(50, 1) As Variant

rankArray(0,0) = "Mike"
rankArray(0,1) = 10
rankArray(1,0) = "John"
rankArray(1,1) = 3

Then I have to report the top 5 scores which is the reason I'm trying to sort the array.  Help! :-\

-mario
0
Comment
Question by:mbosch
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9723313
is this a school assignment?

AW
0
 
LVL 1

Author Comment

by:mbosch
ID: 9723453
huh? no... its a job assignment... I KNOW ITS SIMPLE!  I'm just an idiot :-D
0
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 20 total points
ID: 9723483
Mostly like it

8->

http://www.vb-helper.com/tut1.htm


Just give you a hint how to sort the single dimension array but you should be able to do it for 2 dimenision array
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:MichaelDS
MichaelDS earned 20 total points
ID: 9723859
I cheat when i do this i process the list into a single array and then sort it and they process it back. it is not eigant but it works

Sub Test()
Dim oSortIt As clsSearchSort
Dim rankArray(1, 1) As Variant
Dim SortMe(0 To 1) As Variant
Dim lngBreakAt As Long
Dim i As Long
rankArray(0, 0) = "Mike"
rankArray(0, 1) = 10
rankArray(1, 0) = "John"
rankArray(1, 1) = 3


For i = 0 To 1
  ' Note I added spaces to the number to make it sort Correctly
  SortMe(i) = rankArray(i, 0) & "~" & Right$(Space(6) & rankArray(i, 1), 6)
Next
'Now Sort

Set oSortIt = New clsSearchSort
oSortIt.BubbleSortVariantArray SortMe()
Set oSortIt = Nothing


' now put things back
For i = 0 To 1
  lngBreakAt = InStr(1, SortMe(i), "~", vbTextCompare)
  rankArray(i, 0) = Left$(SortMe(i), lngBreakAt - 1)
  rankArray(i, 1) = Mid$(SortMe(i), lngBreakAt + 1)
Next

End Sub

0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 60 total points
ID: 9723938
if you don't mind the fact that a Bubble sort is probaly NOT the best choice, it is easy to understand:

Public Sub SortArray(rankArray as Variant, n as Integer)

Dim TempI as Integer
Dim TempName as String
Dim i as Integer
Dim j as Integer
dim swap as Boolean

swap = True
Do while swap
   swap = false
   for i = 0 to n-2
      for j = i to n-1
        if rankArray(j,1) > rankArray(i,1) then
            TempName = rankArray(i,0)
            TempI = rankArray(i,1)
             rankArray(i,0) = rankArray(j.0)
             rankArray(i,1) = rankArray(j,1)
             rankArray(j,0) = TempName
             rankArray(j,1) = TempI
             swap = True
        end If
     Next
  Next
Loop
End Sub

That should do it.

AW
 

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9723956
note that MikeDS's soultion is in .NET, not VB 6, if that makes a difference.

AW
0
 
LVL 3

Expert Comment

by:MichaelDS
ID: 9725867
My code works in VB6, VBA not tried in Net? O I used my public Sort Class as I figured the issue was dealing with mult-demensioned array not the basic sort. I suppose that creates a confussion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now