2012-03-14 195 views
1

我有一個字典(字符串,項目)的字典,我試圖按項目名稱按字母順序排序。我不想使用排序的詞典,如果沒有它,我的運氣就沒有了。 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

回答

4

A Dictionary(Of TKey, TValue)本質上是一種無序的類型。沒有辦法對其中一個進行排序。請嘗試使用SortedDictionary(Of TKey, TValue)

Dim map = new SortedDictionary(Of String, Item)() 
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