# simple two-dimensional array sort

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
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
is this a school assignment?

AW
Author Commented:
huh? no... its a job assignment... I KNOW ITS SIMPLE!  I'm just an idiot :-D
Commented:
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
Commented:
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

Commented:
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

Experts Exchange Solution brought to you by