Tuple<T1,T2,T3,T4>.IComparable.CompareTo(Object) 메서드

정의

현재 Tuple<T1,T2,T3,T4> 개체를 지정된 개체와 비교하고 현재 개체가 정렬 순서에 지정된 개체보다 이전인지, 이후인지 또는 같은 위치인지를 나타내는 정수를 반환합니다.

 virtual int System.IComparable.CompareTo(System::Object ^ obj) = IComparable::CompareTo;
int IComparable.CompareTo (object obj);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo

매개 변수

obj
Object

현재 인스턴스와 비교할 개체입니다.

반환

Int32

다음 표와 같이 정렬 순서에서 이 인스턴스와 obj의 상대적 위치를 나타내는 부호 있는 정수입니다.

설명
음의 정수 이 인스턴스가 obj 앞에 오는 경우
0 이 인스턴스와 obj의 위치가 정렬 순서에서 같은 경우
양의 정수 이 인스턴스가 obj 다음에 오는 경우

구현

예외

objTuple<T1,T2,T3,T4> 개체가 아닙니다.

예제

다음 예제에서는 야구 투수의 Tuple<T1,T2,T3,T4> 이름, 투구한 이닝 수, 주어진 안타 및 획득 실행 횟수로 구성되는 개체의 배열을 만듭니다. 배열의 각 튜플 구성 요소를 정렬되지 않은 순서로 표시하고 배열을 정렬한 다음 호출 ToString 하여 각 튜플을 정렬된 순서로 표시합니다. 출력은 배열이 첫 번째 구성 요소인 이름을 기준으로 정렬되었음을 보여줍니다. 이 예제에서는 메서드를 IComparable.CompareTo(Object) 직접 호출하지 않습니다. 이 메서드는 배열의 각 요소에 Sort(Array) 대 한 메서드에 의해 암시적으로 호출됩니다.

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      Tuple<string, decimal, int, int>[] pitchers  =  
                      { Tuple.Create("McHale, Joe", 240.1m, 221, 96),
                        Tuple.Create("Paul, Dave", 233.1m, 231, 84), 
                        Tuple.Create("Williams, Mike", 193.2m, 183, 86),
                        Tuple.Create("Blair, Jack", 168.1m, 146, 65), 
                        Tuple.Create("Henry, Walt", 140.1m, 96, 30),
                        Tuple.Create("Lee, Adam", 137.2m, 109, 45),
                        Tuple.Create("Rohr, Don", 101.0m, 110, 42) };

      // Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());
      Console.WriteLine();
      
      // Sort the array
      Array.Sort(pitchers);
      
      // Display the array in sorted order.
      Console.WriteLine("The values in sorted order:");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());
   }
}
// The example displays the following output;
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Williams, Mike, 193.2, 183, 86)
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (Rohr, Don, 101, 110, 42)
//       
//       The values in sorted order:
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Rohr, Don, 101, 110, 42)
//       (Williams, Mike, 193.2, 183, 86)
open System

let pitchers  =  
    [| Tuple.Create("McHale, Joe", 240.1m, 221, 96)
       Tuple.Create("Paul, Dave", 233.1m, 231, 84)
       Tuple.Create("Williams, Mike", 193.2m, 183, 86)
       Tuple.Create("Blair, Jack", 168.1m, 146, 65)
       Tuple.Create("Henry, Walt", 140.1m, 96, 30)
       Tuple.Create("Lee, Adam", 137.2m, 109, 45)
       Tuple.Create("Rohr, Don", 101.0m, 110, 42) |]

// Display the array in unsorted order.
printfn "The values in unsorted order:"
for pitcher in pitchers do
    printfn $"{pitcher}"
printfn ""

// Sort the array
Array.Sort pitchers

// Display the array in sorted order.
printfn "The values in sorted order:"
for pitcher in pitchers do
    printfn $"{pitcher}"
// The example displays the following output
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Williams, Mike, 193.2, 183, 86)
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (Rohr, Don, 101, 110, 42)
//       
//       The values in sorted order:
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Rohr, Don, 101, 110, 42)
//       (Williams, Mike, 193.2, 183, 86)
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim pitchers() =  
                { Tuple.Create("McHale, Joe", 240.1d, 221, 96),
                  Tuple.Create("Paul, Dave", 233.1d, 231, 84), 
                  Tuple.Create("Williams, Mike", 193.2d, 183, 86),
                  Tuple.Create("Blair, Jack", 168.1d, 146, 65), 
                  Tuple.Create("Henry, Walt", 140.1d, 96, 30),
                  Tuple.Create("Lee, Adam", 137.2d, 109, 45),
                  Tuple.Create("Rohr, Don", 101.0d, 110, 42) }

      ' Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:")
      For Each pitcher In pitchers
         Console.WriteLine(pitcher.ToString())
      Next
      Console.WriteLine()
      
      ' Sort the array
      Array.Sort(pitchers)
      
      ' Display the array in sorted order.
      Console.WriteLine("The values in sorted order:")
      For Each pitcher In pitchers
         Console.WriteLine(pitcher.ToString())
      Next
   End Sub
End Module
' The example displays the following output:
'       The values in unsorted order:
'       (McHale, Joe, 240.1, 221, 96)
'       (Paul, Dave, 233.1, 231, 84)
'       (Williams, Mike, 193.2, 183, 86)
'       (Blair, Jack, 168.1, 146, 65)
'       (Henry, Walt, 140.1, 96, 30)
'       (Lee, Adam, 137.2, 109, 45)
'       (Rohr, Don, 101, 110, 42)
'       
'       The values in sorted order:
'       (Blair, Jack, 168.1, 146, 65)
'       (Henry, Walt, 140.1, 96, 30)
'       (Lee, Adam, 137.2, 109, 45)
'       (McHale, Joe, 240.1, 221, 96)
'       (Paul, Dave, 233.1, 231, 84)
'       (Rohr, Don, 101, 110, 42)
'       (Williams, Mike, 193.2, 183, 86)

설명

이 멤버는 명시적 인터페이스 멤버 구현이며, Tuple<T1,T2,T3,T4> 인스턴스가 IComparable 인터페이스로 캐스팅된 경우에만 사용할 수 있습니다.

이 메서드는 클래스에 IComparable.CompareTo 대한 구현을 Tuple<T1,T2,T3,T4> 제공합니다. 메서드를 직접 호출할 수 있지만 컬렉션의 멤버 순서와 같은 Array.Sort(Array) SortedList.Add컬렉션 정렬 메서드의 기본 오버로드에서 가장 일반적으로 호출됩니다.

주의

IComparable.CompareTo 메서드는 정렬 작업에 사용하기 위한 것입니다. 비교의 주요 목적이 두 개체가 같은지 여부를 확인하는 경우에는 사용하지 않아야 합니다. 두 개체가 같은지 여부를 확인하려면 메서드를 호출합니다 Equals .

이 메서드는 IComparable.CompareTo 기본 개체 비교자를 사용하여 각 구성 요소를 비교합니다.

적용 대상

추가 정보