CompareTo() method in C#

The CompareTo() method in C# is used to compare two values and returns an integer that indicates their relative position in the sort order. This method is part of the IComparable interface and is available for most built-in data types like integers, strings, and dates.

Syntax

Following is the syntax for using the CompareTo() method −

int result = value1.CompareTo(value2);

Return Value

The CompareTo() method returns the following integer values −

  • 0 = both values are equal
  • Positive integer (typically 1) = the calling value is greater than the parameter
  • Negative integer (typically -1) = the calling value is less than the parameter

Using CompareTo() with Integers

Example

using System;

public class Demo {
   public static void Main() {
      int val1 = 100;
      int val2 = 100;
      int val3 = 50;
      int val4 = 150;
      
      Console.WriteLine("Comparing 100 with 100: " + val1.CompareTo(val2));
      Console.WriteLine("Comparing 100 with 50: " + val1.CompareTo(val3));
      Console.WriteLine("Comparing 100 with 150: " + val1.CompareTo(val4));
   }
}

The output of the above code is −

Comparing 100 with 100: 0
Comparing 100 with 50: 1
Comparing 100 with 150: -1

Using CompareTo() with Strings

Example

using System;

public class StringComparison {
   public static void Main() {
      string str1 = "apple";
      string str2 = "banana";
      string str3 = "apple";
      
      Console.WriteLine("Comparing 'apple' with 'banana': " + str1.CompareTo(str2));
      Console.WriteLine("Comparing 'apple' with 'apple': " + str1.CompareTo(str3));
      Console.WriteLine("Comparing 'banana' with 'apple': " + str2.CompareTo(str1));
   }
}

The output of the above code is −

Comparing 'apple' with 'banana': -1
Comparing 'apple' with 'apple': 0
Comparing 'banana' with 'apple': 1

Using CompareTo() with DateTime

Example

using System;

public class DateComparison {
   public static void Main() {
      DateTime date1 = new DateTime(2023, 12, 25);
      DateTime date2 = new DateTime(2023, 12, 31);
      DateTime date3 = new DateTime(2023, 12, 25);
      
      Console.WriteLine("Dec 25 vs Dec 31: " + date1.CompareTo(date2));
      Console.WriteLine("Dec 25 vs Dec 25: " + date1.CompareTo(date3));
      Console.WriteLine("Dec 31 vs Dec 25: " + date2.CompareTo(date1));
   }
}

The output of the above code is −

Dec 25 vs Dec 31: -1
Dec 25 vs Dec 25: 0
Dec 31 vs Dec 25: 1

Comparison Table

Data Type Comparison Logic Example
Integer Numeric value comparison 100.CompareTo(50) returns 1
String Lexicographical (alphabetical) comparison "apple".CompareTo("banana") returns -1
DateTime Chronological comparison Earlier date returns -1 when compared to later

Conclusion

The CompareTo() method provides a standardized way to compare values in C#. It returns 0 for equal values, a positive integer when the calling value is greater, and a negative integer when the calling value is smaller, making it useful for sorting algorithms and conditional logic.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements