繁体   English   中英

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

[英]VB.NET: order dictionary alphabetically

我有一个Dictinary(字符串,项目),我正在尝试按项目名称按字母顺序对其进行排序。 我不想使用排序的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

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

暂无
暂无

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

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