Dictionary的排序问题

juventus821023 2010-01-09 02:53:14
定义了一个Dictionary,其中的key和value数目不确定
key的顺序不规则
比如:
Dim dict As Dictionary(Of Integer, String)
dict.Add(4, "value_four")
dict.Add(1, "value_one")
dict.Add(3, "value_three")
dict.Add(2, "value_two")

现在我想让它按照key来升序排列

如果是一次元的List,可以用Sort
Dim list As List(Of Integer) = New List(Of Integer)
list.Add(1)
list.Add(3)
list.Add(2)
list.Sort()

请问有没有类似于Sort的方法让Dictionary按照key来升序排列?

谢谢
...全文
207 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mngzilin 2010-01-09
  • 打赏
  • 举报
回复
SortedList和SortedDictionary会自动排序

这是SortedList得例子:

            SortedList<int, String>  d=new SortedList<int, String>();
d.Add(4, "value_four");
d.Add(1, "value_one");
d.Add(3, "value_three");
d.Add(2, "value_two");

wuyq11 2010-01-09
  • 打赏
  • 举报
回复
使用SortedDictionary <TKey,TValue>
juventus821023 2010-01-09
  • 打赏
  • 举报
回复
有没有别的更好的方法呢?
古今多少事 2010-01-09
  • 打赏
  • 举报
回复
将dict中元素复制到数组中,利用数组排序功能,排完序再存回Dict.

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧