VB.NET:按字母顺序排列字典



我有一个Dictionary(Of String,Item),我正试图按项目名称按字母顺序排序。我不想使用排序字典,如果没有它,我就没有运气了。林克不是我的强项。。。

Public Class Item
    Public Property name As String
    Public Property size As String
    Public Property price As Decimal
    Public Property timeMachine As Boolean
End Class

Dictionary(Of TKey, TValue)本质上是一种无序类型。没有办法对一个进行排序。请尝试使用SortedDictionary(Of TKey, TValue)

Dim map = new SortedDictionary(Of String, Item)()

(1)获取关键字列表,(2)对关键字进行排序,(3)根据排序后的关键字获取字典值

Dim keys As List(Of String) = dictionary.Keys.ToList
keys.Sort()
For Each str As String In Keys
  Console.WriteLine("{0} -> {1}", str, dictionary.Item(str))
Next

最新更新