locked
FAQs:怎么排序一个多维数组? RRS feed

答案

  •       .NET 数组类已经定义了一个Sort 排序方法,但是,Sort 方法仅支持一维数组排序。

    有时候你想要排序2维或者多维数组(2种类型:锯齿型的和常规型的)的话,你就需要用不同的方法去排序他们了。以下是简单的示例:

     

    1.           这段代码定义了一个锯齿型的2维数组,并排序。重载了Array.Sort 方法,增加 了一个参数,在这段代码中,增加的一个参数是一个对象,该对象所属的类继承了IComparer 接口。

     

    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

                ' xy是整数数组类型。

               

                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.          以下的代码定义了一个常规的2维数组,并排序。该方法通过创建一个新的一维数组(由原始数组的行数组成),IComparer.Compare 方法用创建的一维数组的元素去排序2维数组。

     

    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}

            ' 初始化 comparer 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

        Private sortArray(,) As Integer

        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 y是整型,代表数组的第几行

            Dim i1 As Integer = DirectCast(x, Integer)

            Dim i2 As Integer = DirectCast(y, Integer)

             Return sortArray(i1, 1).CompareTo(sortArray(i2, 1))

        End Function

    End Class

    3.       使用LINQ

     

    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
    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月3日 2:41