按类值对VB.net列表进行排序 [英] Sorting a VB.net List by a class value

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

问题描述

我有一个列表(即Dim nList as new List(of className)).每个类都有一个名为zIndex(即className.zIndex)的属性.是否可以通过列表中所有元素中的zIndex变量对列表中的元素进行排序?

I have a list (i.e. Dim nList as new List(of className)). Each class has a property named zIndex (i.e. className.zIndex). Is it possible to sort the elements of the list by the zIndex variable in all of the elements of the list?

推荐答案

假定您拥有LINQ:

Sub Main()
    Dim list = New List(Of Person)()
    'Pretend the list has stuff in it
    Dim sorted = list.OrderBy(Function(x) x.zIndex)
End Sub

Public Class Person
    Public Property zIndex As Integer
End Class

或者如果LINQ不是你的事

Or if LINQ isn't your thing:

Dim list = New List(Of Person)()
list.Sort(Function(x, y) x.zIndex.CompareTo(y.zIndex))
'Will sort list in place

LINQ提供了更大的灵活性;例如,如果您想订购多个商品,则可以使用ThenBy.它还使语法更加简洁.

LINQ offers more flexibility; such as being able to use ThenBy if you want to order by more than one thing. It also makes for a slightly cleaner syntax.

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

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