1,105,208 Community Members

Sorting a Multidimensional Array

Member Avatar
Quinncunx
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, Is there at all a way to sort (numerically) a multidimensional array?

I have an array that is as follows

`dim points(23,1) as String`

The first dimension of the array has the players names in, the second dimension of it has their cumulative points, so I want to sort the array by points and maintaing the correlation of players name to points (if that makes sense)

thanks in advance!

Member Avatar
Begginnerdev
Veteran Poster
1,141 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
1
 

You could try a different method and define a structure.....

Dim stud(24) As Students
Dim tmpStud As Students


Structure Students
    Dim StudentName As String
    Dim CumulativeScores As Double
End Structure




    For i = 0 To stud.Count - 2
        If stud(i).CumulativeScores > stud(i + 1).CumulativeScores Then
            tmpStud = stud(i + 1)
            stud(i + 1) = stud(i)
            stud(i) = tmpStud
        End If
    Next
Member Avatar
Reverend Jim
Noli mentula
5,387 posts since Aug 2010
Reputation Points: 744 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
2
 

Or you could use a SortedList and not have to do the sort yourself.

Dim mylist As New SortedList

mylist.Add("John", 23)
mylist.Add("Jim", 14)
mylist.Add("Fred", 92)

For Each key As String In mylist.Keys
    Debug.WriteLine(key & ": " & mylist(key))
Next
Member Avatar
Quinncunx
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the input guys, I assume it would be possible to populate the sorted list from a multidimensional array?

EDIT:

Thanks Jim, once again you have provided a useful way of solving a problem.

Also thanks BegginnerDev for your input

Member Avatar
Reverend Jim
Noli mentula
5,387 posts since Aug 2010
Reputation Points: 744 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

One final comment on SortedLists. If the item you want to sort is not a primitive data type (String, Integer, etc) then you have to specify a user-written function that compares two elements and returns a value of -1, 0, or 1. This custum function is then used by SortedList to do the sort. You would need to do this if (as BeginnerDev suggested) you created a structure to hold your data.

If you have your answer then please mark this thread as solved.

Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

Hi
If Sortedlist doesn't work for you you can also try a DataTable. A DataTable has a DefaultView property, which is a DataView for the table and a DataView has a Sort property which allows you to sort your data by any column in either direction e.g. "PlayerPoints Desc"

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: