mike mcintyre's

.N e t J o u r n a l

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Topics

Source Code

EDIT: As someone pointed out my example was a bit sloppy so it has been edited. In Step One, the Person class has been changed to avoid the need to cast from object to person in the CompareTo method. This was accomplished by using the generic IComparable interface. Also, string concatenation in the CompareTo method was eliminated to avoid possible performance drag. Finally, the result of the LastName comparrison is cached in a variable name result to improve performance. In Step Two, a generic List(of Person) is used instead of an ArrayList. The example download has been updated too.

 

What must you do to sort your custom classes?

 

The code sample below shows one technique which is to implement the IComparable interface in your classes.

 

Note: A more extensive example showing how to add sortability to custom classes is available as a FREE Visual Basic 2005 project. Download à Add Sortability to Visual Basic Classes

 

Step One – Implement the IComparable interface in a class.

 

 

Public Class Person

    Implements IComparable(Of Person)

 

    ' LastName Property

    Private m_LastName As String

    Public Property LastName() As String

        Get

            Return m_LastName

        End Get

        Set(ByVal value As String)

            m_LastName = value

        End Set

    End Property

 

    ' FirstName Property

    Private m_FirstName As String

    Public Property FirstName() As String

        Get

            Return m_FirstName

        End Get

        Set(ByVal value As String)

            m_FirstName = value

        End Set

    End Property

 

    ' Constructor

    Public Sub New(ByVal lastName As String, ByVal firstName As String)

        Me.LastName = lastName

        Me.FirstName = firstName

    End Sub

 

    Public Function CompareTo(ByVal otherPerson As Person) As Integer Implements System.IComparable(Of Person).CompareTo

        ' The person comparison depends on the comparison of the

        '     the underlying string values. Because the CompareTo method is

        '     strongly typed, it is not necessary to test for the correct

        '     object type.

        Dim result As Integer = Me.LastName.CompareTo(otherPerson.LastName)

        If result <> 0 Then

            Return result

        Else

            Return Me.FirstName.CompareTo(otherPerson.FirstName)

        End If

    End Function

End Class

 

Step Two – Sort

 

        ' Create an ArrayList containing Person objects.

        Dim m_PersonsList As New List(Of Person)

        m_PersonsList.Add(New Person("McCan", "Mike"))

        m_PersonsList.Add(New Person("Kim", "Joe"))

        m_PersonsList.Add(New Person("Jones", "John"))

        m_PersonsList.Add(New Person("Jones", "Arthur"))

        m_PersonsList.Add(New Person("Jones", "Fred"))

 

        ' Sort the Persons List

        m_PersonsList.Sort()

 

posted on Sunday, August 06, 2006 3:35 PM