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!

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

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))
commented: Nice, I didn't even think of SortedList +0

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


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

Also thanks BegginnerDev for your input

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.

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"

Function Sort2DimArray(SA As Array, sc0 As Integer, Optional sc1 As Integer = -1, Optional sc2 As Integer = -1)
   'SA Array to sort sc0,sc1,sc2 Cols to sort
   Dim cols As Integer = SA.GetLength(1) - 1
    Dim rows As Integer = SA.GetLength(0) - 1
    Dim na(rows, cols) As String
    Dim a(rows) As String
    Dim b(rows) As Integer
    Dim c As Integer = 1
    If sc1 > -1 Then c = c + 1
    If sc2 > -1 Then c = c + 1
    For x = 0 To rows
        If c = 1 Then a(x) = b(sc0)
        If c = 2 Then a(x) = b(sc0) & b(sc1)
        If c = 3 Then a(x) = b(sc0) & b(sc1) & b(sc2)
        b(x) = x
    Array.Sort(a, b)
    For x = 0 To rows
        For y = 0 To cols
            na(x, y) = SA(b(x), y)
    Sort2DimArray = na
End Function

works gfreat

commented: Be timely. -3
commented: Undocumented code with bad variable names. How useful. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.