Generic.List - sorting

Siig

New member
Joined
Feb 12, 2008
Messages
1
Programming Experience
3-5
Hi all,

Can anyone tell me how to sort a generic.list of objects where my sort method is two properties.

Here is an examble:
VB.NET:
Public Class Person
    Public Age As Integer
    Public Name As String

    Public Sub New(ByVal iAge As Integer, ByVal sName As String)
        Me.Age = iAge
        Me.Name = sName
    End Sub
End Class

Public Class Main

    Public Sub New()
        Dim Persons As New Generic.List(Of Person)


        Persons.Add(New Person(23, "Sam"))
        Persons.Add(New Person(24, "Allan"))
        Persons.Add(New Person(23, "John"))
        Persons.Add(New Person(23, "Jim"))
        Persons.Add(New Person(28, "Jack"))

        For Each p As Person In Persons
            Console.WriteLine(p.Age & vbTab & p.Name)
        Next
    End Sub
End Class

This would give me the following output:
23 Sam
24 Allan
23 John
23 Jim
28 Jack

I would like to have the list sorted by first age and then name
which should give me the folling output:
23 Jim
23 John
23 Sam
24 Allan
28 Jack


Regards
Stefan
 
Have a read through this thread, it talks about and give examples for IComparable(Of T) and IComparer(Of T).
 
Back
Top