本文介绍了如何在VB.Net中对System.Collections.Generic.List进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个通用列表(m_equipmentList),它是对象的集合(Schedule_Payitem).
如何根据子对象的属性对列表进行排序?

I using a genric list(m_equipmentList ) which is collection of objects (Schedule_Payitem).
How can sort list according to a proerty of child object ?

Dim m_equipmentList As New List(Of Schedule_Payitem)

需要根据Schedule_Payitem的resourceid属性对m_equipmentList进行排序.

Need to sort m_equipmentList on basis of resourceid property of Schedule_Payitem.

推荐答案

您是否正在使用VB9?如果是这样,我会使用 lambda表达式创建Comparer(Of Schedule_PayItem).否则,编写一个简短的类来实现IComparer(Of Schedule_PayItem).通过您进入List.Sort的任何人.

Are you using VB9? If so, I'd use a lambda expression to create a Comparer(Of Schedule_PayItem). Otherwise, write a short class to implement IComparer(Of Schedule_PayItem). pass whichever one you've got into List.Sort.

lambda表达式的示例(未经测试):

An example for the lambda expression (untested):

m_equipmentList.Sort(Function(p1, p2) p1.ResourceID.CompareTo(p2.ResourceID))

对于IComparer(Of Schedule_PayItem):

Public Class PayItemResourceComparer
    Implements IComparer(Of Schedule_PayItem)
    Public Function Compare(ByVal p1 As Schedule_PayItem, _
                            ByVal p2 As Schedule_PayItem) As Integer
        Return p1.ResourceID.CompareTo(p2.ResourceID)
    End Function
End Class

...

m_equipmentList.Sort(New PayItemResourceComparer)

这篇关于如何在VB.Net中对System.Collections.Generic.List进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:02
查看更多