在 VB.NET 中对多维数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43178649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:14:07  来源:igfitidea点击:

Sort Multidimensional Array in VB.NET

.netarraysstringvb.netmultidimensional-array

提问by nsssayom

I have a 2X50 array like this-

我有一个像这样的 2X50 阵列-

R-125212,11
C-254645,25
R-456598,96
M-456878,35
O-980857,89
And so on...

Now I want to sort this array with the values of the 2nd Column. So the output should look like-

现在我想用第二列的值对这个数组进行排序。所以输出应该是这样的——

R-125212,11
C-254645,25
M-456878,35
O-980857,89
R-456598,96
And so on...

How to do this with VB.NET easily? If there is other better way to have the similar result without using array, that also will help me.

如何使用 VB.NET 轻松做到这一点?如果有其他更好的方法可以在不使用数组的情况下获得类似的结果,那也会对我有所帮助。

回答by SSS

There are many possible solutions to your question, but in my experience the best is to use a System.Data.DataTable:

您的问题有很多可能的解决方案,但根据我的经验,最好的方法是使用System.Data.DataTable

Dim dtb As New System.Data.DataTable
dtb.Columns.Add("Column1")
dtb.Columns.Add("Column2", GetType(Integer))
dtb.Rows.Add("Z-123456", 2)
dtb.Rows.Add("R-125212", 11)
dtb.Rows.Add("C-254645", 25)
dtb.Rows.Add("R-456598", 96)
dtb.Rows.Add("M-456878", 35)
dtb.Rows.Add("O-980857", 89)
Dim dvw As DataView = dtb.DefaultView
dvw.Sort = "Column2 ASC"
Dim dtbSorted As DataTable = dvw.ToTable()
DataGridView1.DataSource = dtbSorted

回答by 3vts

I would recommend the use of a List(Of Tuple)instead of an array. It is more dynamic. Please check this code:

我建议使用 aList(Of Tuple)而不是数组。它更具动态性。请检查此代码:

Sub SortList()
    'Declare the List Of Tuple with a Tuple of Char, Integer, Integer
    Dim lstToSort As New List(Of Tuple(Of Char, Integer, Integer))
    'Example to Add items
    lstToSort.Add(Tuple.Create("R"c, 250645, 11))
    lstToSort.Add(Tuple.Create("C"c, 125212, 25))
    'Sort is just 1 line
    lstToSort = lstToSort.OrderBy(Function(i) i.Item2).ToList
    'Loop through the elements to print them
    For Each tpl As Tuple(Of Char, Integer, Integer) In lstToSort
        Console.WriteLine(tpl.Item1 & "-" & tpl.Item2 & "," & tpl.Item3)
    Next
End Sub

Edit: Given your edit on the question here is the code fixed:

编辑:鉴于您对这里问题的编辑,代码已修复:

Sub SortList()
    'Declare the List Of Tuple with a tuple of String, Integer
    Dim lstToSort As New List(Of Tuple(Of String, Integer))
    'Example to add items
    lstToSort.Add(Tuple.Create("R-250645", 11))
    lstToSort.Add(Tuple.Create("C-125212", 25))
    'Sort is just 1 line
    lstToSort = lstToSort.OrderBy(Function(i) i.Item2).ToList
    'Loop through the elements to print them
    For Each tpl As Tuple(Of String, Integer) In lstToSort
        Console.WriteLine(tpl.Item1 & "," & tpl.Item2)
    Next
End Sub

Give it a try and let me know your comments

试一试,让我知道你的意见

回答by Randy

Public Function Sort2DimArray(SA As Array, order As Boolean, sc0 As Integer, Optional sc1 As Integer = -1, Optional sc2 As Integer = -1) As Array 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

Public Function Sort2DimArray(SA As Array, order As Boolean, sc0 As Integer, Optional sc1 As Integer = -1, Optional sc2 As Integer = -1) As Array 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如果 sc2 > -1 那么 c = c + 1

    For x = 0 To rows
        If c = 1 Then a(x) = SA(x, sc0)
        If c = 2 Then a(x) = SA(x, sc0) & SA(x, sc1)
        If c = 3 Then a(x) = SA(x, sc0) & SA(x, sc1) & SA(x, sc2)
        b(x) = x
    Next
    Array.Sort(a, b)
    If order = False Then
        For x = 0 To rows
            For y = 0 To cols
                na(x, y) = SA(b(x), y)
            Next
        Next
    Else
        For x = 0 To rows
            For y = 0 To cols
                na(rows - x, y) = SA(b(x), y)
            Next
        Next
    End If
    Sort2DimArray = na
End Function