BigInteger.CompareTo 方法

定義

比較這個執行個體的值與另一個值,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於另一個值。

多載

CompareTo(Int64)

比較這個執行個體與帶正負號的 64 位元整數,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於帶正負號 64 位元整數的值。

CompareTo(BigInteger)

比較這個執行個體與第二個 BigInteger,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於指定之物件的值。

CompareTo(Object)

比較這個執行個體與特定物件,並且傳回一個整數,指出這個執行個體的值是小於、等於或大於特定物件的值。

CompareTo(UInt64)

比較這個執行個體與不帶正負號的 64 位元整數,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於不帶正負號 64 位元整數的值。

CompareTo(Int64)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

比較這個執行個體與帶正負號的 64 位元整數,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於帶正負號 64 位元整數的值。

public:
 int CompareTo(long other);
public int CompareTo (long other);
member this.CompareTo : int64 -> int
Public Function CompareTo (other As Long) As Integer

參數

other
Int64

要比較的帶正負號 64 位元整數。

傳回

帶正負號的整數值,表示這個執行個體與 other 的關聯性,如下表所示。

傳回值 描述
小於零 目前的執行個體小於 other
目前的執行個體等於 other
大於零 目前的執行個體大於 other

範例

下列範例說明使用整數值呼叫 CompareTo(Int64) 方法的結果。

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
Dim bigIntValue As BigInteger = BigInteger.Parse("3221123045552")

Dim byteValue As Byte = 16
Dim sbyteValue As SByte = -16
Dim shortValue As Short = 1233      
Dim ushortValue As UShort = 1233
Dim intValue As Integer = -12233
Dim uintValue As UInteger = 12233
Dim longValue As Long = 12382222
Dim ulongValue As Integer = 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

備註

如果 otherByteInt16Int32SByteUInt16UInt32 值,則會在呼叫 方法時 CompareTo(Int64) 隱含轉換成 Int64 值。

適用於

CompareTo(BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

比較這個執行個體與第二個 BigInteger,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於指定之物件的值。

public:
 virtual int CompareTo(System::Numerics::BigInteger other);
public int CompareTo (System.Numerics.BigInteger other);
abstract member CompareTo : System.Numerics.BigInteger -> int
override this.CompareTo : System.Numerics.BigInteger -> int
Public Function CompareTo (other As BigInteger) As Integer

參數

other
BigInteger

要比較的物件。

傳回

帶正負號的整數值,表示這個執行個體與 other 的關聯性,如下表所示。

傳回值 描述
小於零 目前的執行個體小於 other
目前的執行個體等於 other
大於零 目前的執行個體大於 other

實作

範例

下列範例說明如何使用 CompareTo(BigInteger) 方法來排序物件清單 StarInfo 。 每個 StarInfo 物件都會提供星號的名稱及其距離以英哩為單位的資訊。 StarInfo 會實作 IComparable<T> 介面,這個介面可讓 StarInfo 物件依泛型集合類別排序。 其 IComparable<T>.CompareTo 實作只會包裝 對 的 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);
   }
}
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

下列程式碼接著會具現化四 StarInfo 個物件,並將其儲存在泛型 List<T> 物件中。 List<T>.Sort呼叫 方法之後, StarInfo 物件會依其與地球距離的順序顯示。

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)
Module Example
   Public Sub Main()
      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
         Console.WriteLine(star)
      Next   
   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)

備註

這個方法的多 CompareTo 載會實作 IComparable<T>.CompareTo 方法。 泛型集合物件會使用它來排序集合中的專案。

另請參閱

適用於

CompareTo(Object)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

比較這個執行個體與特定物件,並且傳回一個整數,指出這個執行個體的值是小於、等於或大於特定物件的值。

public:
 virtual int CompareTo(System::Object ^ obj);
public int CompareTo (object? obj);
public int CompareTo (object obj);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
Public Function CompareTo (obj As Object) As Integer

參數

obj
Object

要比較的物件。

傳回

帶正負號的整數,表示目前執行個體與 obj 參數的關聯性,如下表所示。

傳回值 描述
小於零 目前的執行個體小於 obj
目前的執行個體等於 obj
大於零 目前執行個體大於 obj,或者 obj 參數為 null

實作

例外狀況

範例

下列範例會呼叫 方法, CompareTo(Object) 以比較 BigInteger 值與物件陣列中的每個專案:

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
Dim values() As Object = { BigInteger.Pow(Int64.MaxValue, 10), Nothing, 
                           12.534, Int64.MaxValue, BigInteger.One }
Dim number As BigInteger = UInt64.MaxValue

For Each value As Object In values
   Try
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, 
                        number.CompareTo(value))
   Catch e As ArgumentException
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value)
   End Try                     
Next                                 
' 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

備註

這個方法的多 CompareTo 載會實作 IComparable.CompareTo 方法。 非泛型集合物件會使用它來排序集合中的專案。

參數 obj 必須是下列其中一項:

  • 執行時間型別為 BigInteger 的物件。

  • 值為 Objectnull 的變數。 如果 參數的值 objnull ,則方法會傳回 1,表示目前的實例大於 obj

另請參閱

適用於

CompareTo(UInt64)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

重要

此 API 不符合 CLS 規範。

比較這個執行個體與不帶正負號的 64 位元整數,並且傳回整數,這個整數表示這個執行個體的值小於、等於或大於不帶正負號 64 位元整數的值。

public:
 int CompareTo(System::UInt64 other);
[System.CLSCompliant(false)]
public int CompareTo (ulong other);
[<System.CLSCompliant(false)>]
member this.CompareTo : uint64 -> int
Public Function CompareTo (other As ULong) As Integer

參數

other
UInt64

要比較的不帶正負號 64 位元整數。

傳回

帶正負號的整數,表示這個執行個體與 other 的相對值,如下表所示。

傳回值描述
小於零目前的執行個體小於 other
目前的執行個體等於 other
大於零目前的執行個體大於 other
屬性

適用於