troubleshooting Question

Sorting Generic List in Visual Basic .Net

Avatar of Ignyte_Software
Ignyte_Software asked on
.NET ProgrammingVisual Basic.NET
12 Comments1 Solution682 ViewsLast Modified:
I have a generic list that is made up of a object, instead of something simple like a string:

Dim familyMember As New List(Of Person)

I want to sort the list using the .Sort method, but I cannot figure out how to sort based on the properties of the object used for the list.  Below is some sample code:

    Public Sub CreateFamily()

        Dim familyMember As New List(Of Person)
        Dim person As Person

        person = New Person("Bill", "", "Clinton", "111-111-1111")
        familyMember.Add(person)

        person = New Person("Hillary", "", "Clinton", "222-222-2222")
        familyMember.Add(person)

        person = New Person("Chelsea", "", "Clinton", "333-333-3333")
        familyMember.Add(person)

        familyMember.Sort(?, ?)

    End Sub

Public Class Person

    Private _FirstName As String
    Private _MiddleName As String
    Private _LastName As String
    Private _PhoneNumber As String

    'Initalize a new instance of the person class
    Public Sub New(ByVal firstName As String, ByVal middleName As String, ByVal lastName As String, ByVal phoneNumber As String)

        _FirstName = firstName
        _MiddleName = middleName
        _LastName = lastName
        _PhoneNumber = phoneNumber

    End Sub

    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get

        Set(ByVal Value As String)
            _FirstName = Value
        End Set
    End Property

    Public Property MiddleName() As String
        Get
            Return _MiddleName
        End Get

        Set(ByVal Value As String)
            _MiddleName = Value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _LastName
        End Get

        Set(ByVal Value As String)
            _LastName = Value
        End Set
    End Property

    Public Property PhoneNumber() As String
        Get
            Return _PhoneNumber
        End Get

        Set(ByVal Value As String)
            _PhoneNumber = Value
        End Set
    End Property

End Class

Using this example, how would I use the .Sort to sort by lastName and then by firstName?

        familyMember.Sort(?, ?)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros