VB.net,如何按值对收集项目进行排序 [英] VB.net, How to sort collection items by value

查看:78
本文介绍了VB.net,如何按值对收集项目进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在VB.NET中按值对收集项进行排序?

How do I sort collection items by value in VB.NET?

我要排序:

Dim col as Collection = New Collection
col.Add("b","b1")
col.Add("a","a1")
col.Add("d","d1")

推荐答案

就像@Krishnadditya提到的那样,集合不是用于排序的理想选择,因为它们包含类型为Object的项目,该项目过于宽泛,无法相互比较.如果您还没有结婚,可以使用LINQ查询列表或任何可枚举的对象来完成此操作

Like @Krishnadditya mentioned, Collections aren't ideal for sorting because they contain items of type Object which is too genereic to be useful in comparing against each other. If you weren't married to a collection, you can do this with a LINQ query to a list or anything that can be cast an enumerable

Dim list = {
        New With {.Object = "b", .Key = "b1"},
        New With {.Object = "a", .Key = "a1"},
        New With {.Object = "d", .Key = "d1"}}

Dim sortedList = _
        From item In list
        Order By item.Key

这篇关于VB.net,如何按值对收集项目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆