【问题标题】:Specific sorting of a multidimensional array多维数组的具体排序
【发布时间】:2013-08-14 18:51:17
【问题描述】:

我目前正在将一个 VBA 项目翻译成 VB.NET。 我对数据表的排序有一点问题。

我的桌子看起来像:

... | ... | Firstname3 | Name3 | Pay3 | Firstname1 | Name1 | Pay1 | Firstname2 | Name2 | Pay2 |...
... | ... | Firstname2 | Name2 | Pay2 | Firstname3 | Name3 | Pay3 | Firstname1 | Name1 | Pay1 |...

等等...我将数据表中所需的 56 列导出到一个数组中,并尝试在名称上对其进行水平排序。我在 VBA 中就是这样做的:

Public Sub SortTable(ByRef aggTab(,) As Object, ByVal columnToSortOn As Integer, ByVal lowerValue As Byte,
                    ByVal upperValue As Byte)

    Dim ref As Object = aggTab((lowerValue + upperValue) \ 2, columnToSortOn)
    Dim refLowerValue As Byte = lowerValue
    Dim refUpperValue As Byte = upperValue
    Dim temp As Object

    Do
        Do While aggTab(refLowerValue, columnToSortOn) < ref
            refLowerValue = refLowerValue + 1
        Loop
        Do While ref < aggTab(refUpperValue, columnToSortOn)
            refUpperValue = refUpperValue - 1
        Loop
        If refLowerValue <= refUpperValue Then
            For i = LBound(aggTab, 2) To UBound(aggTab, 2)
                temp = aggTab(refLowerValue, i)
                aggTab(refLowerValue, i) = aggTab(refUpperValue, i)
                aggTab(refUpperValue, i) = temp
            Next i
            refLowerValue = refLowerValue + 1 : refUpperValue = refUpperValue - 1
        End If
    Loop While refLowerValue <= refUpperValue

    If refLowerValue < upperValue Then Call SortTable(aggTab, columnToSortOn, refLowerValue, upperValue)
    If lowerValue < refUpperValue Then Call SortTable(aggTab, columnToSortOn, lowerValue, refUpperValue)
End Sub

但是当我将代码转换为 VB.NET 时,它无法正常工作。 有谁可以解释我为什么?因为在excel中它工作得很好。

【问题讨论】:

  • 我不会说法语,但究竟是什么不能正常工作。似乎您对自己的东西进行了分类,这可以使用 vb.net 的集合轻松完成。你只要把东西扔进去,VB就会为你分类...msdn.microsoft.com/en-us/library/f7fta44c.aspx
  • 无需说法语就知道了 ;) 转换后的代码正在排序,但绝对不是必须的.​​..缺少一些列...然后我将查看 vb 的集合.
  • 如果可以,您应该将 aggTab 转换为类列表。它会让你的生活变得更轻松。

标签: arrays vb.net vba sorting datatable


【解决方案1】:

举个例子;

    Dim openWith As New SortedDictionary(Of String, String)

    ' Add some elements to the dictionary. There are no  
    ' duplicate keys, but some of the values are duplicates.
    openWith.Add("txt", "notepad.exe")
    openWith.Add("bmp", "paint.exe")
    openWith.Add("dib", "paint.exe")
    openWith.Add("rtf", "wordpad.exe")

    For Each Ext As KeyValuePair(Of String, String) In openWith
        TextBox1.AppendText(Ext.Key & " " & Ext.Value & vbCrLf)
    Next

这将自动对“扩展名”进行排序。

【讨论】:

  • 这正是我在您之前的评论之后采取的。再次感谢。
猜你喜欢
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 2020-03-20
相关资源
最近更新 更多