BigInteger.CompareTo Method

Definition

Compares the value of this instance with another value and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the other value.

Overloads

CompareTo(Int64)

Compares this instance to a signed 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the signed 64-bit integer.

CompareTo(BigInteger)

Compares this instance to a second BigInteger and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

CompareTo(Object)

Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

CompareTo(UInt64)

Compares this instance to an unsigned 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the unsigned 64-bit integer.

CompareTo(Int64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Compares this instance to a signed 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the signed 64-bit integer.

public int CompareTo (long other);

Parameters

other
Int64

The signed 64-bit integer to compare.

Returns

A signed integer value that indicates the relationship of this instance to other, as shown in the following table.

Return value Description
Less than zero The current instance is less than other.
Zero The current instance equals other.
Greater than zero The current instance is greater than other.

Examples

The following example illustrates the result of calling the CompareTo(Int64) method with integral values.

BigInteger bigIntValue = BigInteger.Parse("3221123045552");

byte byteValue = 16;
sbyte sbyteValue = -16;
short shortValue = 1233;
ushort ushortValue = 1233;
int intValue = -12233;
uint uintValue = 12233;
long longValue = 12382222;
ulong ulongValue = 1238222;

Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, byteValue,
                  bigIntValue.CompareTo(byteValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, sbyteValue,
                  bigIntValue.CompareTo(sbyteValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, shortValue,
                  bigIntValue.CompareTo(shortValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, ushortValue,
                  bigIntValue.CompareTo(ushortValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, intValue,
                  bigIntValue.CompareTo(intValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, uintValue,
                  bigIntValue.CompareTo(uintValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, longValue,
                  bigIntValue.CompareTo(longValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, ulongValue,
                  bigIntValue.CompareTo(ulongValue));
// The example displays the following output:
//       Comparing 3221123045552 with 16: 1
//       Comparing 3221123045552 with -16: 1
//       Comparing 3221123045552 with 1233: 1
//       Comparing 3221123045552 with 1233: 1
//       Comparing 3221123045552 with -12233: 1
//       Comparing 3221123045552 with 12233: 1
//       Comparing 3221123045552 with 12382222: 1
//       Comparing 3221123045552 with 1238222: 1

Remarks

If other is a Byte, Int16, Int32, SByte, UInt16, or UInt32 value, it is implicitly converted to an Int64 value when the CompareTo(Int64) method is called.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CompareTo(BigInteger)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Compares this instance to a second BigInteger and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

public int CompareTo (System.Numerics.BigInteger other);

Parameters

other
BigInteger

The object to compare.

Returns

A signed integer value that indicates the relationship of this instance to other, as shown in the following table.

Return value Description
Less than zero The current instance is less than other.
Zero The current instance equals other.
Greater than zero The current instance is greater than other.

Implements

Examples

The following example illustrates the use of the CompareTo(BigInteger) method to order a list of StarInfo objects. Each StarInfo object provides information about a star's name and its distance from the Earth in miles. StarInfo implements the IComparable<T> interface, which enables StarInfo objects to be sorted by generic collection classes. Its IComparable<T>.CompareTo implementation just wraps a call to CompareTo(BigInteger).

using System;
using System.Collections.Generic;
using System.Numerics;

public struct StarInfo : IComparable<StarInfo>
{
   // Define constructors.
   public StarInfo(string name, double lightYears)
   {
      this.Name = name;

      // Calculate distance in miles from light years.
      this.Distance = (BigInteger) Math.Round(lightYears * 5.88e12);
   }

   public StarInfo(string name, BigInteger distance)
   {
      this.Name = name;
      this.Distance = distance;
   }

   // Define public fields.
   public string Name;
   public BigInteger Distance;

   // Display name of star and its distance in parentheses.
   public override string ToString()
   {
      return String.Format("{0,-10} ({1:N0})", this.Name, this.Distance);
   }

   // Compare StarInfo objects by their distance from Earth.
   public int CompareTo(StarInfo other)
   {
      return this.Distance.CompareTo(other.Distance);
   }
}

The following code then instantiates four StarInfo objects and stores them in a generic List<T> object. After the List<T>.Sort method is called, StarInfo objects are displayed in order of their distance from the Earth.

public class Example
{
   public static void Main()
   {
      StarInfo star;
      List<StarInfo> stars = new List<StarInfo>();

      star = new StarInfo("Sirius", 8.6d);
      stars.Add(star);
      star = new StarInfo("Rigel", 1400d);
      stars.Add(star);
      star = new StarInfo("Castor", 49d);
      stars.Add(star);
      star = new StarInfo("Antares", 520d);
      stars.Add(star);

      stars.Sort();

      foreach (StarInfo sortedStar in stars)
         Console.WriteLine(sortedStar);
   }
}
// The example displays the following output:
//       Sirius     (50,568,000,000,000)
//       Castor     (288,120,000,000,000)
//       Antares    (3,057,600,000,000,000)
//       Rigel      (8,232,000,000,000,000)

Remarks

This overload of the CompareTo method implements the IComparable<T>.CompareTo method. It is used by generic collection objects to order the items in the collection.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CompareTo(Object)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

public int CompareTo (object? obj);
public int CompareTo (object obj);

Parameters

obj
Object

The object to compare.

Returns

A signed integer that indicates the relationship of the current instance to the obj parameter, as shown in the following table.

Return value Description
Less than zero The current instance is less than obj.
Zero The current instance equals obj.
Greater than zero The current instance is greater than obj, or the obj parameter is null.

Implements

Exceptions

Examples

The following example calls the CompareTo(Object) method to compare a BigInteger value with each element in an object array:

object[] values = { BigInteger.Pow(Int64.MaxValue, 10), null,
                    12.534, Int64.MaxValue, BigInteger.One };
BigInteger number = UInt64.MaxValue;

foreach (object value in values)
{
   try {
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value,
                        number.CompareTo(value));
   }
   catch (ArgumentException) {
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value);
   }
}
// The example displays the following output:
//    Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1
//    Comparing 18446744073709551615 with '': 1
//    Unable to compare the Double value 12.534 with a BigInteger.
//    Unable to compare the Int64 value 9223372036854775807 with a BigInteger.
//    Comparing 18446744073709551615 with '1': 1

Remarks

This overload of the CompareTo method implements the IComparable.CompareTo method. It is used by non-generic collection objects to order the items in the collection.

The obj parameter must be one of the following:

  • An object whose run-time type is BigInteger.

  • An Object variable whose value is null. If the value of the obj parameter is null, the method returns 1, which indicates that the current instance is greater than obj.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CompareTo(UInt64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Important

This API is not CLS-compliant.

Compares this instance to an unsigned 64-bit integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the unsigned 64-bit integer.

[System.CLSCompliant(false)]
public int CompareTo (ulong other);

Parameters

other
UInt64

The unsigned 64-bit integer to compare.

Returns

A signed integer that indicates the relative value of this instance and other, as shown in the following table.

Return valueDescription
Less than zeroThe current instance is less than other.
ZeroThe current instance equals other.
Greater than zeroThe current instance is greater than other.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0