次の方法で共有


IComparable<T>.CompareTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Function CompareTo ( _
    other As T _
) As Integer
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.

Remarks

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 nulla null reference (Nothing in Visual Basic), 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.

Examples

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.

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.