.NET Framework Class Library
StringCompareTo Method (Object)

Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Function CompareTo ( _
	value As [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_0_0_0%] _
) As [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_0_0_1%]
public [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_1_0_0%] CompareTo(
	[%$TOPIC/fkw3h78a_en-us_VS_110_3_0_1_0_1%] value
)
public:
virtual [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_2_0_0%] CompareTo(
	[%$TOPIC/fkw3h78a_en-us_VS_110_3_0_2_0_1%]^ value
) sealed
abstract CompareTo : 
        value:[%$TOPIC/fkw3h78a_en-us_VS_110_3_0_3_0_0%] -> [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_3_0_1%]  
override CompareTo : 
        value:[%$TOPIC/fkw3h78a_en-us_VS_110_3_0_3_0_2%] -> [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_3_0_3%]
public final function CompareTo(
	value : [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_4_0_0%]
) : [%$TOPIC/fkw3h78a_en-us_VS_110_3_0_4_0_1%]

Parameters

value
Type: SystemObject

An object that evaluates to a String.

Return Value

Type: SystemInt32
A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter.

Value

Condition

Less than zero

This instance precedes value.

Zero

This instance has the same position in the sort order as value.

Greater than zero

This instance follows value.

-or-

value is .

Implements

IComparableCompareTo(Object)
Exceptions
ExceptionCondition
ArgumentException

value is not a String.

Remarks

value must be a String object.

Caution noteCaution

The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.GlobalizationCompareOptions.

For more information about the behavior of this method, see the Remarks section of the StringCompare(String, String) method.

Notes to Callers

Character sets include ignorable characters. The CompareTo method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.

Module Example
   Public Sub Main()
      Dim s1 As String = "ani" + ChrW(&h00AD) + "mal" 
      Dim o1 As Object = "animal"

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, o1, s1.CompareTo(o1))
  End Sub 
End Module 
' The example displays the following output: 
'       Comparison of 'ani-mal' and 'animal': 0
using System;

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      object o1 = "animal";

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, o1, s1.CompareTo(o1));
   }
}
// The example displays the following output: 
//       Comparison of 'ani-mal' and 'animal': 0

To recognize ignorable characters in a string comparison, call the CompareOrdinal(String, String) method.

Examples

The following example uses the CompareTo method with an Object. Because it attempts to compare a String instance to a TestClass object, the method throws an ArgumentException.

Public Class TestClass
End Class  


Public Class Example
   Public Shared Sub Main()
      Dim test As New TestClass()
      Dim objectsToCompare() As Object = { test, test.ToString(), 123,
                                           123.ToString(), "some text",
                                           "Some Text" }
      Dim s As String = "some text" 
      For Each objectToCompare In objectsToCompare
         Try 
            Dim i As Integer = s.CompareTo(objectToCompare)
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i)
         Catch e As ArgumentException
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name)
         End Try 
      Next 
   End Sub  
End Class  
' The example displays the following output: 
'       Bad argument: TestClass (type TestClass) 
'       Comparing 'some text' with 'TestClass': -1 
'       Bad argument: 123 (type Int32) 
'       Comparing 'some text' with '123': 1 
'       Comparing 'some text' with 'some text': 0 
'       Comparing 'some text' with 'Some Text': -1
using System;

public class TestClass
{}

public class Example 
{
   public static void Main()
   {
      var test = new TestClass();
      Object[] objectsToCompare = { test, test.ToString(), 123,
                                    123.ToString(), "some text",
                                    "Some Text" };
      string s = "some text";
      foreach (var objectToCompare in objectsToCompare) {
         try {
            int i = s.CompareTo(objectToCompare);
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i);
         }
         catch (ArgumentException) {
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name);
         }
      }
   }
}
// The example displays the following output: 
//    Error: System.ArgumentException: Object must be of type String. 
//       at System.String.CompareTo(Object value) 
//       at Example.Main()
using namespace System;

public ref class TestClass{};

int main()
{
   TestClass^ test = gcnew TestClass;
   array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
                                        (123).ToString(), "some text",
                                        "Some Text" };
   String^ s = "some text";
   for each (Object^ objectToCompare in objectsToCompare) {
      try {
         Int32 i = s->CompareTo(objectToCompare);
         Console::WriteLine("Comparing '{0}' with '{1}': {2}",
                            s, objectToCompare, i);
      }
      catch (ArgumentException^ e) {
         Console::WriteLine("Bad argument: {0} (type {1})",
                            objectToCompare,
                            objectToCompare->GetType()->Name);
      }
   }
}
// The example displays the following output: 
//       Error: System.ArgumentException: Object must be of type String. 
//          at System.String.CompareTo(Object value) 
//          at main()
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8