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

时间:2012-03-14 06:03:41

标签: vb.net linq sorting dictionary

我有一个Dictinary(Of String,Item),我正在尝试按项目名称按字母顺序对其进行排序。我不想使用排序的dictinary而没有它,我没有运气。 Linq不是我的强项......

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

2 个答案:

答案 0 :(得分:4)

Dictionary(Of TKey, TValue)本身就是一种无序类型。没有办法整理一个。请尝试使用SortedDictionary(Of TKey, TValue)

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

答案 1 :(得分:0)

(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