example - vb.net array sort




如何在VB.Net中對System.Collections.Generic.List進行排序? (3)

我使用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)

嘗試這個

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 );
              });

我不知道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()





sorting