我使用genric列表(m_equipmentList),它是对象的集合(Schedule_Payitem).
如何根据子对象的权利排序列表?
Dim m_equipmentList As New List(Of Schedule_Payitem)
需要根据Schedule_Payitem的resourceid属性对m_equipmentList进行排序.
你在用VB9吗?如果是这样,我会使用lambda表达式创建一个Comparer(Of Schedule_PayItem)
.否则,写一个简短的类来实现IComparer(Of Schedule_PayItem)
.通过你进入List.Sort的任何一个.
lambda表达式的示例(未经测试):
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,所以我在C#中做到了
m_equipmentList.Sort( (payItem1,payItem2)=>payItem1.ResourceID.CompareTo(payItem2.ResourceID));
并使用反射器将其翻译为vb.net希望它有所帮助
m_equipmentList.Sort( Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) Return payItem1.ResourceID.CompareTo(payItem2.ResourceID) End Function)
或者您可以从IComparable继承Schedule_Payitem并实现CompareTo然后只需调用 m_equipmentList.Sort()
您可以通过更改以下内容按降序完成对列表的排序 -
m_equipmentList.Sort( Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) Return payItem1.ResourceID.CompareTo(payItem2.ResourceID) End Function)
对此
m_equipmentList.Sort( Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) Return payItem2.ResourceID.CompareTo(payItem1.ResourceID) End Function)