Int64.CompareTo Method in C# with Examples

The Int64.CompareTo() method in C# is used to compare the current long instance with a specified object or Int64 value and returns an integer indicating their relative values. This method is essential for sorting operations and value comparisons.

Syntax

The Int64.CompareTo() method has two overloads −

public int CompareTo(long value);
public int CompareTo(object value);

Parameters

  • value − In the first overload, it's a long integer to compare. In the second overload, it's an object to compare, which must be null or an instance of Int64.

Return Value

The method returns an integer that indicates the relative position of the instances in the sort order −

  • Less than zero − This instance is less than the specified value

  • Zero − This instance is equal to the specified value

  • Greater than zero − This instance is greater than the specified value

CompareTo Return Values < 0 This instance is smaller = 0 Both values are equal > 0 This instance is greater

Using CompareTo with Long Values

This example demonstrates comparing two long values directly −

using System;

public class Demo {
   public static void Main() {
      long val1 = 20;
      long val2 = 18;
      Console.WriteLine("Value 1 = " + val1);
      Console.WriteLine("Value 2 = " + val2);
      Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
   }
}

The output of the above code is −

Value 1 = 20
Value 2 = 18
Return value (comparison) = 1

Using CompareTo with Object Parameter

This example shows how to use the object overload of CompareTo()

using System;

public class Demo {
   public static void Main() {
      long val1 = 20;
      object val2 = (long)20;
      Console.WriteLine("Value 1 = " + val1);
      Console.WriteLine("Value 2 = " + val2);
      Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
   }
}

The output of the above code is −

Value 1 = 20
Value 2 = 20
Return value (comparison) = 0

Using CompareTo for Sorting

The CompareTo() method is commonly used in sorting scenarios −

using System;

public class Demo {
   public static void Main() {
      long[] numbers = { 100, 50, 75, 25 };
      
      Console.WriteLine("Original array:");
      foreach (long num in numbers) {
         Console.Write(num + " ");
      }
      
      Array.Sort(numbers);
      
      Console.WriteLine("\nSorted array:");
      foreach (long num in numbers) {
         Console.Write(num + " ");
      }
      
      // Manual comparison examples
      Console.WriteLine("<br>\nComparison examples:");
      Console.WriteLine("50.CompareTo(75) = " + ((long)50).CompareTo(75));
      Console.WriteLine("75.CompareTo(50) = " + ((long)75).CompareTo(50));
      Console.WriteLine("25.CompareTo(25) = " + ((long)25).CompareTo(25));
   }
}

The output of the above code is −

Original array:
100 50 75 25 
Sorted array:
25 50 75 100 

Comparison examples:
50.CompareTo(75) = -1
75.CompareTo(50) = 1
25.CompareTo(25) = 0

Conclusion

The Int64.CompareTo() method provides a reliable way to compare long values, returning negative, zero, or positive integers based on the comparison result. It's essential for sorting operations and implementing custom comparison logic in applications.

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

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements