views:

13830

answers:

4

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)

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

A: 

Try this

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 );
              });
cmsjr
VB.Net doesn't have anonymous delegate support until vs2008, so the conversion from this code may not be trivial.
Joel Coehoorn
+1  A: 

I don't know vb.net so I did it in C#

m_equipmentList.Sort(
   (payItem1,payItem2)=>payItem1.ResourceID.CompareTo(payItem2.ResourceID));

and using the reflector translated it to vb.net hope it helps

m_equipmentList.Sort(
Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) 
    Return payItem1.ResourceID.CompareTo(payItem2.ResourceID)
End Function)

or you can inherit Schedule_Payitem from IComparable and implement CompareTo and then just call m_equipmentList.Sort()

pablito
+13  A: 

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.

An example for the lambda expression (untested):

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

And for the 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)
Jon Skeet
Helpful tip to specify Collections.Generic.IComparer(Of Schedule_PayItem). I was getting an error: 'System.Collections.IComparer' has no type parameters. A little searching helped me find that two such interfaces with same name exist, one in System.Collections and another in System.Collections.Generic.
Kyle B.
A: 

You may want to look at the blog below for a much more flexible class that can be implemented through the sort method of a list class. The list can contain objects of any class and be sorted on as many properties of the contained class as you want. Each property can be independently sorted in ascending or descending order and the sort charaterics of the types of each property will be respected (i.e., depenging on the type characterics of the properties listed in the sort string, the method can mix date sorting, numeric sorting and string sorting within a single call to the method).

http://blog.josh420.com/archives/2009/02/sorting-generic-list-dynamically-in-vbnet.aspx

I have not banged on the code to determine if it handles all possible error conditions and, because it uses reflection, it may not perform ideally for very long lists, but it definitely is very flexible.

Richard
This link now dead
Marc Gravell