Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a Generic List

Status
Not open for further replies.

Juice05

Programmer
Dec 4, 2001
247
US
I have a Generic List of Invoices (Custom class). Each Invoice has a header class and a list of lineitems, which is also a class.

How can i sort the List of invoices by a value that is found in the header class?

Invoice 1
HeaderInfo - InvoiceID DateCreated CreatedBy
LineItemInfo - CustomerID Amount
LineItemInfo - CustomerID Amount
LineItemInfo - CustomerID Amount
Invoice 2
HeaderInfo - InvoiceID DateCreated CreatedBy
LineItemInfo - CustomerID Amount
LineItemInfo - CustomerID Amount

I would like to sort my Invoice list by the DateCreated in the header of each invoice.
 
I think you should be able to use the IComparer class and implement that.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yep, implement IComparer(of T). Something like this:
Code:
Public Class InvoiceSorter
    Implements IComparer(of Invoice)

    Public Function Compare(ByVal x As Invoice, ByVal y As Invoice) As Integer Implements IComparer(Of Invoice).Compare

        Return x.HeaderInfo.DateCreated.CompareTo(y.HeaderInfo.DateCreated)

    End Function
End Class

usage:
Code:
Dim myList as new List(of Invoice)()

'add invoices to myList...

myList.Sort(new InvoiceSorter())

Don't you love Generics?


[blue]_______________________________________[/blue]
Business Logic:"AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle AND..." - Dr. Suess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top