简体   繁体   English

有没有一种方法可以对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 http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx

Say i want to to do for each on each key and I want keys that are alphabetically smaller like "BR" to show up first. 说我想为每个键上的每个键做一个操作,并且我希望像字母“ BR”这样按字母顺序排列的较小键首先出现。

Is there a way to do so? 有办法吗?

Or is dictionary strictly key object pair? 还是字典严格是关键对象对?

I found this link as a solution 我找到此链接作为解决方案

http://www.dotnetperls.com/sort-dictionary-vbnet http://www.dotnetperls.com/sort-dictionary-vbnet

Basically insert the keys into a list and then sort that list and then enumerate based on that list. 基本上将键插入列表,然后对该列表进行排序,然后根据该列表进行枚举。

I copy paste the sample code 我复制粘贴示例代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM