locked
How do I sort a multidimensional array? RRS feed

Answers

  • The .NET Array class defines a Sort method, but this method just supports sorting one-dimensional arrays. Sometimes you need to sort a two-dimensional (or N-dimensional) array. Multi-dimensional arrays have two kinds:  jagged arrays and rectangular arrays. You need to use different strategies to sort the arrays. Take a look at the following code samples for your reference.

     

    1)   The code snippet defines a jagged array and sorts this two- dimensional array by using the overloaded versions of Array.Sort to pass as a parameter an object that implements the IComparer interface.

     

        Dim intarr1() As Integer = {33, 12}
        Dim intarr2() As Integer = {12, 10}
        Dim intarr3() As Integer = {13, 5}
        Dim intarr4() As Integer = {27, 19}
        Dim jaggedarr()() As Integer = _
        {intarr1, intarr2, intarr3, intarr4}
        Dim myComparer As New JaggedComparer
        Array.Sort(jaggedarr, myComparer)
    
        Class JaggedComparer
            Implements IComparer
            Public Function Compare(ByVal x As Object, ByVal y As Object) _
            As Integer Implements IComparer.Compare
                ' x and y are arrays of integers
                ' sort on the 2nd item in each array
                Dim a1() As Integer = DirectCast(x, Integer())
                Dim a2() As Integer = DirectCast(y, Integer())
                Return a1(1).CompareTo(a2(1))
            End Function
        End Class
        
    

     

    2)   The code snippet shows how to declare a two-dimensional rectangular array and sort it. I created a second, one-dimensional array that contains the row numbers of the rectangular array. The IComparer.Compare method uses the row numbers in one-dimensional array to sort the items in two-dimensional array to implement this.

     

    Public Class Form1
        Private Sub sort_rectangular_Click(ByVal sender As System.Object, _
                                           ByVal e As System.EventArgs) _
                                           Handles sort_rectangular.Click
            Dim a(,) As Integer = {{33, 12}, {12, 10}, {13, 5}, {27, 19}}
            Dim tagArray() As Integer = {0, 1, 2, 3}
            ' Initialize the comparer and sort
            Dim myComparer As New RectangularComparer(a)
            Array.Sort(tagArray, myComparer)
            Dim i As Integer
            For i = 0 To tagArray.Length – 1
                Console.WriteLine(a(tagArray(i), 1))
            Next
        End Sub
    End Class
    Class RectangularComparer
        Implements IComparer
        ' maintain a reference to the 2-dimensional array being sorted
        Private sortArray(,) As Integer
        ' constructor initializes the sortArray reference
        Public Sub New(ByVal theArray(,) As Integer)
            sortArray = theArray
        End Sub
        Public Function Compare(ByVal x As Object, ByVal y As Object) _
        As Integer Implements IComparer.Compare
            ' x and y are integer row numbers into the sortArray
            Dim i1 As Integer = DirectCast(x, Integer)
            Dim i2 As Integer = DirectCast(y, Integer)
            ' compare the items in the sortArray
            Return sortArray(i1, 1).CompareTo(sortArray(i2, 1))
        End Function
    End Class
    
    

     

     

    3)   Using LINQ to query the array and sort the result.

     

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button1.Click
            Dim intarr1() As Integer = {33, 12, 2, 45}
            Dim intarr2() As Integer = {12, 10, 3, 33}
            Dim intarr3() As Integer = {13, 5, 4, 234}
            Dim intarr4() As Integer = {27, 19, 6, 11}
            Dim a()() As Integer = {intarr1, intarr2, intarr3, intarr4}
            Dim sortarr = From num In a
            Order By num(2) Descending _
            Select num(2)
            For Each i In sortarr
                Console.WriteLine(i.ToString)
            Next
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As 
    System.EventArgs) Handles Button2.Click
            Dim a(,) As Integer = {{33, 12, 2, 45}, {12, 10, 3, 33}, _
                                   {13, 5, 4, 234}, {27, 19, 6, 11}}
            Dim tagArray() As Integer = {0, 1, 2, 3}
            Dim sortarr = From num In tagArray
            Order By a(num, 1) Descending _
            Select a(num, 1)
            For Each i In sortarr
                Console.WriteLine(i.ToString)
            Next
        End Sub
    End Class
    
    

     

     

    Related threads:

    http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/68c30d3f-6eaf-4b00-8d21-3737267ec71a/

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/2519170e-b8ed-4253-ae40-d74f58cc0e60/

     

    For more FAQ about Visual Basic Express, please see Visual Basic Express FAQ

     

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 9, 2009 1:12 PM