Share via


BigInteger.CompareTo Method (BigInteger)

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

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.

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

Syntax

'Declaration
Public Function CompareTo ( _
    other As BigInteger _
) As Integer
public int CompareTo(
    BigInteger other
)

Parameters

Return Value

Type: System.Int32
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

IComparable<T>.CompareTo(T)

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.

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).

Imports System.Collections.Generic
Imports System.Numerics

Public Structure StarInfo : Implements IComparable(Of StarInfo)
   ' Define constructors.
   Public Sub New(ByVal name As String, ByVal lightYears As Double)
      Me.Name = name
      ' Calculate distance in miles from light years.
      Me.Distance = CType(Math.Round(lightYears * 5880000000000.0), BigInteger)
   End Sub

   Public Sub New(ByVal name As String, ByVal distance As BigInteger)
      Me.Name = name
      Me.Distance = distance
   End Sub

   ' Define public fields.
   Public Name As String
   Public Distance As BigInteger

   ' Display name of star and its distance in parentheses.
   Public Overrides Function ToString() As String
      Return String.Format("{0,-10} ({1})", Me.Name, Me.Distance)
   End Function

   ' Compare StarInfo objects by their distance from Earth.
   Public Function CompareTo(ByVal other As StarInfo) As Integer _
                   Implements IComparable(Of StarInfo).CompareTo
      Return Me.Distance.CompareTo(other.Distance)
   End Function
End Structure
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})", 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.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim star As StarInfo
      Dim stars As New List(Of 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()

      For Each star In stars
         outputBlock.Text &= star.ToString() + vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       Sirius     (50568000000000)
'       Castor     (288120000000000)
'       Antares    (3057600000000000)
'       Rigel      (8232000000000000)
public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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)
         outputBlock.Text += sortedStar + "\n";
   }
}
// The example displays the following output:
//       Sirius     (50568000000000)
//       Castor     (288120000000000)
//       Antares    (3057600000000000)
//       Rigel      (8232000000000000)

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.