Export (0) Print
Expand All

IComparable<T>.CompareTo Method

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

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

int CompareTo(
	T other
)

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<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 null, 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<T> object to create a collection of addresses. The List<T> object uses the IComparable<T> implementation to sort the list entries, which are then displayed in sorted order.


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";

   }
}


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