我正在使用一个通用列表(m_equipmentList),它是对象(Schedule_Payitem)的集合。
如何根据子对象的属性对列表进行排序?
Dim m_equipmentList As New List(Of Schedule_Payitem)
需要根据 Schedule_Payitem 的 resourceid 属性对 m_equipmentList 进行排序。
我正在使用一个通用列表(m_equipmentList),它是对象(Schedule_Payitem)的集合。
如何根据子对象的属性对列表进行排序?
Dim m_equipmentList As New List(Of Schedule_Payitem)
需要根据 Schedule_Payitem 的 resourceid 属性对 m_equipmentList 进行排序。
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)
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)
试试这个
Dim m_equipmentList As New List(Of Schedule_Payitem)
m_equipmentList.Sort(delegate(Schedule_Payitem p1, Schedule_Payitem p2)
{
return p1.resourceid .CompareTo(p2.resourceid );
});