Windows Dev Center

IComparable(Of T) Interface

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering instances.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Interface IComparable(Of In T)

Type Parameters

In T

The type of objects to compare.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see [2678dc63-c7f9-4590-9ddc-0a4df684d42e].

The IComparable(Of T) type exposes the following members.

  NameDescription
Public methodCompareToCompares the current object with another object of the same type.
Top

This interface is implemented by types whose values can be ordered; for example, the numeric and string classes. A value type or class implements the CompareTo method to create a type-specific comparison method suitable for purposes such as sorting.

NoteNote:

The IComparable(Of T) interface defines the CompareTo method, which determines the sort order of instances of the implementing type. The IEquatable(Of T) interface defines the Equals method, which determines the equality of instances of the implementing type.

The IComparable(Of T) interface provides a strongly typed comparison method for ordering members of a generic collection object. Because of this, it is usually not called directly from developer code. Instead, it is called automatically by sorting methods such as the List(Of T).Sort method.

Notes to Implementers

Replace the type parameter of the IComparable(Of T) interface with the type that is implementing this interface.

The following code example illustrates the implementation of IComparable for a simple Temperature object.


Public Class Address : Implements IComparable(Of Address)

   Private addr1, addr2, cty, st, coun, postal As String

   Public Sub New(address1 As String, address2 As String, city As String, _
                  state As String, postal As String, country As String)
      Me.addr1 = address1
      Me.addr2 = address2
      Me.cty = city
      Me.st = state
      Me.coun = country
      Me.postal = postalCode      
   End Sub

   Public Property Address1 As String
      Get
         Return Me.addr1
      End Get
      Set
         Me.addr1 = value
      End Set
   End Property

   Public Property Address2 As String
      Get
         Return Me.addr2
      End Get
      Set
         Me.addr2 = value
      End Set
   End Property

   Public Property City As String
      Get
         Return Me.cty
      End Get
      Set
         Me.cty = value
      End Set
   End Property

   Public Property State As String
      Get
         Return Me.st
      End Get
      Set
         Me.st = value
      End Set
   End Property

   Public Property Country As String
      Get
         Return Me.coun
      End Get
      Set
         Me.coun = value
      End Set
   End Property

   Public Property PostalCode As String
      Get
         Return Me.postal
      End Get
      Set
         Me.postal = value
      End Set
   End Property

   Public Overrides Function ToString() As String
      Return addr1 & vbCrLf & CStr(IIf(Not String.IsNullOrEmpty(addr2), addr2 & vbCrLf, "")) _
             & cty & ", " & st & " " & postal & " " & coun
   End Function

   Public Function CompareTo(other As Address) As Integer _
       Implements IComparable(Of Address).CompareTo

      ' If other is Nothing, this instance is greater.
      If other Is Nothing Then Return 1

      Dim otherAddress As String = other.Country & other.State & other.PostalCode & _
                                   other.City & other.Address1 & other.Address2
      Return (coun & st & postal & cty & addr1 & addr2).CompareTo(otherAddress)
   End Function
End Class

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim addresses As New List(Of Address)
      addresses.Add(New Address("106 East 5th St.", "", "New City", "MI", "48002", "USA"))
      addresses.Add(New Address("47 East End Rd.", "", "Huxenplux", "NJ", "20203", "USA"))
      addresses.Add(New Address("12043 N.E. 72nd St.", "", "Belleville", "WA", "98101", "USA"))
      addresses.Sort()
      For Each address As Address In addresses
         outputBlock.Text &= address.ToString() & vbCrLf & vbCrLf
      Next
   End Sub
End Module


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft