在 VB.NET 中对对象列表进行排序 [英] Sort a List of Object in VB.NET

查看:38
本文介绍了在 VB.NET 中对对象列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个乘客(对象)列表,其中有一个不同的属性..

I have a list of passengers(object) where it has a differents properties..

passenger.name
passenger.age
passenger.surname

我想按年龄标准对这个列表进行排序,我该怎么做?

And I want to sort this list by age criterion, how can i do this?

我知道在整数/字符串列表中 List.Sort() 有效,但如果是对象列表,我不知道是否可以按对象属性的值排序!

I know in a list of integer/string List.Sort() works, but if is an object list, i dont know if its possible to sort by the value of a object property!

谢谢.

推荐答案

要按对象中的属性排序,您必须指定比较器或获取该属性的方法.

To sort by a property in the object, you have to specify a comparer or a method to get that property.

使用 List.Sort 方法:

theList.Sort(Function(x, y) x.age.CompareTo(y.age))

使用 OrderBy 扩展方法:

theList = theList.OrderBy(Function(x) x.age).ToList()

这篇关于在 VB.NET 中对对象列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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