BigInteger.CompareTo Method (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.

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

Public Function CompareTo ( _
	other As BigInteger _
) As Integer


Type: System.Numerics.BigInteger
The object to compare.

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


Less than zero

The current instance is less than other.


The current instance equals other.

Greater than zero

The current instance is greater than other.


IComparable(Of T).CompareTo(T)

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

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(Of T) interface, which enables StarInfo objects to be sorted by generic collection classes. Its IComparable(Of 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(name As String, lightYears As Double)
      Me.Name = name
      ' Calculate distance in miles from light years.
      Me.Distance = CType(Math.Round(lightYears * 5.88e12), BigInteger)
   End Sub

   Public Sub New(name As String, 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:N0})", Me.Name, Me.Distance)
   End Function

   ' Compare StarInfo objects by their distance from Earth.
   Public Function CompareTo(other As starInfo) As Integer _
                   Implements IComparable(Of StarInfo).CompareTo
      Return Me.Distance.CompareTo(other.Distance)
   End Function                
End Structure

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

Module Example
   Public Sub Main()
      Dim star As StarInfo
      Dim stars As New List(Of StarInfo)

      star = New StarInfo("Sirius", 8.6d)
      star = New StarInfo("Rigel", 1400d)
      star = New StarInfo("Castor", 49d)
      star = New StarInfo("Antares", 520d)


      For Each star In stars
   End Sub
End Module
' 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)

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft