如何使用VB.NET以相反的顺序对字典的键进行排序?

前端之家收集整理的这篇文章主要介绍了如何使用VB.NET以相反的顺序对字典的键进行排序?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一本字典:

Dim dicItems As Dictionary(of Integer,String)

字典中的项目是:

1,cat
2,dog
3,bird

我希望订单是:

3,bird
2,dog
1,cat

解决方法

您可以使用LINQ轻松解决此问题:

Dim dicItems As New Dictionary(Of Integer,String)
With dicItems
  .Add(1,"cat")
  .Add(2,"dog")
  .Add(3,"bird")
End With

dim query = from item in dicItems
            order by item.Key descending
            select item

如果需要,还可以使用Lambda语法:

Dim query = dicItems.OrderByDescending(Function(item) item.Key)

猜你在找的VB相关文章