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

问题描述:

我有一个字典(字符串,项目)的字典,我试图按项目名称按字母顺序排序。我不想使用排序的词典,如果没有它,我的运气就没有了。 Linq不是我的强项......VB.NET:按字母顺序排序的字典

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 
+0

你应该实现Isortable和其他一些接口或使用获取字典的值SortedDictionary或其他实现这些接口的列表类。 – 2012-03-14 06:06:53

+0

为什么你不想使用SortedDictionary? – Heinzi 2012-03-14 06:09:44

A 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