Export (0) Print
Expand All

IComparable(Of T).CompareTo Method

Compares the current object with another object of the same type.

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

'Declaration
Function CompareTo ( _
	other As T _
) As Integer

Parameters

other
Type: T
An object to compare with this object.

Return Value

Type: System.Int32
A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:

Value

Meaning

Less than zero

This object is less than the other parameter.

Zero

This object is equal to other.

Greater than zero

This object is greater than other.

CompareTo 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 the List(Of T).Sort method.

This method is only a definition and must be implemented by a specific class or value type to have effect. The meaning of the comparisons, "less than," "equal to," and "greater than," depends on the particular implementation.

By definition, any object compares greater than Nothing, and two null references compare equal to each other.

Notes to Implementers

For objects A, B, and C, the following must be true:

A.CompareTo(A) is required to return zero.

If A.CompareTo(B) returns zero, then B.CompareTo(A) is required to return zero.

If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero, then A.CompareTo(C) is required to return zero.

If A.CompareTo(B) returns a value other than zero, then B.CompareTo(A) is required to return a value of the opposite sign.

If A.CompareTo(B) returns a value x that is not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to return a value of the same sign as x and y.

Notes to Callers

Use the CompareTo method to determine the ordering of instances of a class.

The following example illustrates the implementation of IComparable for an Address object. The example uses the generic List(Of T) object to create a collection of addresses. The List(Of T) object uses the IComparable(Of T) implementation to sort the list entries, which are then displayed in sorted order.


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


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft