有没有办法在 vb.net 中对字典中的键进行排序
Is there a way to sort keys in dictionary in vb.net
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx
假设我想在每个键上对每个键执行操作,并且我希望按字母顺序排列较小的键(例如 "BR" 首先显示)。
有办法吗?
或者字典是严格的键对象对?
我发现这个 link 作为解决方案
http://www.dotnetperls.com/sort-dictionary-vbnet
基本上是将键插入到一个列表中,然后对该列表进行排序,然后根据该列表进行枚举。
我复制粘贴示例代码
Sub Main()
' Create Dictionary with string keys.
Dim dict As New Dictionary(Of String, Integer)
dict.Add("dog", 0)
dict.Add("cat", 1)
dict.Add("mouse", 5)
dict.Add("eel", 3)
dict.Add("programmer", 2)
' Get list of keys.
Dim keys As List(Of String) = dict.Keys.ToList
' Sort the keys.
keys.Sort()
' Loop over the sorted keys.
Dim str As String
For Each str In keys
Console.WriteLine("{0} -> {1}", str, dict.Item(str))
Next
End Sub
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx
假设我想在每个键上对每个键执行操作,并且我希望按字母顺序排列较小的键(例如 "BR" 首先显示)。
有办法吗?
或者字典是严格的键对象对?
我发现这个 link 作为解决方案
http://www.dotnetperls.com/sort-dictionary-vbnet
基本上是将键插入到一个列表中,然后对该列表进行排序,然后根据该列表进行枚举。
我复制粘贴示例代码
Sub Main()
' Create Dictionary with string keys.
Dim dict As New Dictionary(Of String, Integer)
dict.Add("dog", 0)
dict.Add("cat", 1)
dict.Add("mouse", 5)
dict.Add("eel", 3)
dict.Add("programmer", 2)
' Get list of keys.
Dim keys As List(Of String) = dict.Keys.ToList
' Sort the keys.
keys.Sort()
' Loop over the sorted keys.
Dim str As String
For Each str In keys
Console.WriteLine("{0} -> {1}", str, dict.Item(str))
Next
End Sub