IComparable<T> Interface
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Interface IComparable(Of In T)
public interface IComparable<in T>
Type Parameters
inT
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<T> type exposes the following members.
Methods
Name | Description | |
---|---|---|
CompareTo | Compares the current object with another object of the same type. |
Top
Remarks
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.
Note: |
---|
The IComparable<T> interface defines the CompareTo method, which determines the sort order of instances of the implementing type. The IEquatable<T> interface defines the Equals method, which determines the equality of instances of the implementing type. |
The IComparable<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<T>.Sort() method.
Notes to Implementers
Replace the type parameter of the IComparable<T> interface with the type that is implementing this interface.
Examples
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
using System;
using System.Collections.Generic;
public class Address : IComparable<Address>
{
private string addr1, addr2, cty, st, coun, postal;
public Address(string address1, string address2, string city,
string state, string postalCode, string country)
{
this.addr1 = address1;
this.addr2 = address2;
this.cty = city;
this.st = state;
this.coun = country;
this.postal = postalCode;
}
public string Address1
{
get { return this.addr1; }
set { this.addr1 = value; }
}
public string Address2
{
get {return this.addr2; }
set { this.addr2 = value; }
}
public string City
{
get { return this.cty; }
set { this.cty = value; }
}
public string State
{
get { return this.st; }
set { this.st = value; }
}
public string Country
{
get { return this.coun; }
set { this.coun = value; }
}
public string PostalCode
{
get {return this.postal; }
set {this.postal = value; }
}
public override string ToString()
{
string addressLine2 = string.IsNullOrEmpty(addr2) ? "" : addr2 + "\n";
return addr1 + "\n" + addressLine2 +
cty + ", " + st + " " + postal + " " + coun;
}
public int CompareTo(Address other)
{
// If other is null, this instance is greater.
if (other == null) return 1;
string otherAddress = other.Country + other.State + other.PostalCode +
other.City + other.Address1 + other.Address2;
return (coun + st + postal + cty + addr1 + addr2).CompareTo(otherAddress);
}
}
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
List<Address> addresses = new List<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();
foreach (Address address in addresses)
outputBlock.Text += address.ToString() + "\n\n";
}
}
Version Information
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
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.