BigInteger.CompareTo 메서드

정의

이 인스턴스의 값을 다른 값과 비교하고 이 인스턴스의 값이 다른 값보다 작은지, 같은지 또는 큰지를 나타내는 정수를 반환합니다.

오버로드

CompareTo(Int64)

이 인스턴스를 부호 있는 64비트 정수와 비교하고 이 인스턴스의 값이 부호 있는 64비트 정수 값보다 작은지, 같은지 또는 큰지를 나타내는 정수를 반환합니다.

CompareTo(BigInteger)

이 인스턴스를 두 번째 BigInteger와 비교하고 이 인스턴스의 값이 지정된 개체의 값보다 작은지, 같은지 또는 큰지를 나타내는 정수를 반환합니다.

CompareTo(Object)

이 인스턴스를 지정된 개체와 비교하고 이 인스턴스의 값이 지정된 개체의 값보다 작은지, 같은지 또는 큰지를 나타내는 정수를 반환합니다.

CompareTo(UInt64)

이 인스턴스를 부호 없는 64비트 정수와 비교하고 이 인스턴스의 값이 부호 없는 64비트 정수 값보다 작은지, 같은지 또는 큰지를 나타내는 정수를 반환합니다.

CompareTo(Int64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 사이의 관계를 나타내는 부호 있는 정수 값입니다.

반환 값 설명
0보다 작음 현재 인스턴스가 other보다 작습니다.
0 현재 인스턴스가 other와 같습니다.
0보다 큼 현재 인스턴스가 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

설명

Byte, , Int16, Int32, SByteUInt16또는 UInt32 값이면 other 메서드가 호출되면 암시적으로 값 CompareTo(Int64) 으로 변환 Int64 됩니다.

적용 대상

CompareTo(BigInteger)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 사이의 관계를 나타내는 부호 있는 정수 값입니다.

반환 값 설명
0보다 작음 현재 인스턴스가 other보다 작습니다.
0 현재 인스턴스가 other와 같습니다.
0보다 큼 현재 인스턴스가 other보다 큽니다.

구현

예제

다음 예제에서는 메서드를 사용하여 CompareTo(BigInteger) 개체 목록을 StarInfo 정렬하는 방법을 보여 줍니다. 각 StarInfo 개체는 star 이름과 지구와의 거리(마일)에 대한 정보를 제공합니다. StarInfoIComparable<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

그런 다음, 다음 코드는 4개의 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)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 매개 변수 사이의 관계를 나타내는 부호 있는 정수입니다.

반환 값 설명
0보다 작음 현재 인스턴스가 obj보다 작습니다.
0 현재 인스턴스가 obj와 같습니다.
0보다 큼 현재 인스턴스가 obj보다 크거나 obj 매개 변수가 null입니다.

구현

예외

obj이(가) BigInteger가 아닌 경우

예제

다음 예제에서는 메서드를 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.

  • 값이 인 Object 변수입니다 null. 매개 변수의 obj 값이 이면 메서드는 null현재 instance 보다 obj크다는 것을 나타내는 1을 반환합니다.

추가 정보

적용 대상

CompareTo(UInt64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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의 상대 값을 나타내는 부호 있는 정수입니다.

반환 값설명
0보다 작음현재 인스턴스가 other보다 작습니다.
0현재 인스턴스가 other와 같습니다.
0보다 큼현재 인스턴스가 other보다 큽니다.
특성

적용 대상