Tuple<T1,T2,T3,T4>.IStructuralComparable.CompareTo 메서드

정의

지정된 비교자를 사용하여 현재 Tuple<T1,T2,T3,T4> 개체와 지정된 개체를 비교하고 정렬 순서에서 현재 개체의 위치가 지정된 개체보다 앞인지, 뒤인지 또는 동일한지를 나타내는 정수를 반환합니다.

 virtual int System.Collections.IStructuralComparable.CompareTo(System::Object ^ other, System::Collections::IComparer ^ comparer) = System::Collections::IStructuralComparable::CompareTo;
int IStructuralComparable.CompareTo (object other, System.Collections.IComparer comparer);
abstract member System.Collections.IStructuralComparable.CompareTo : obj * System.Collections.IComparer -> int
override this.System.Collections.IStructuralComparable.CompareTo : obj * System.Collections.IComparer -> int
Function CompareTo (other As Object, comparer As IComparer) As Integer Implements IStructuralComparable.CompareTo

매개 변수

other
Object

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

comparer
IComparer

비교를 위한 사용자 지정 규칙을 제공하는 개체입니다.

반환

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

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

구현

예외

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

예제

다음 예제에서는 야구 투수에 대한 통계 데이터를 포함하는 개체의 Tuple<T1,T2,T3,T4> 배열을 만듭니다. 데이터 항목에는 투수의 이름, 투구 이닝 수, 투수의 득점 평균(투수가 경기당 허용하는 평균 실행 수), 투수가 포기한 안타 수가 포함됩니다. 이 예제에서는 배열의 각 튜플 구성 요소를 정렬되지 않은 순서로 표시하고 배열을 정렬한 다음 를 호출 ToString 하여 각 튜플의 값을 정렬된 순서로 표시합니다. 배열을 정렬하기 위해 이 예제에서는 인터페이스를 구현 IComparer 하는 제네릭 PitcherComparer 클래스를 정의하고 개체를 첫 번째 구성 요소가 아닌 세 번째 구성 요소(획득한 실행 평균)의 값으로 오름차순으로 정렬 Tuple<T1,T2,T3,T4> 합니다. 예제에서는 메서드를 직접 호출 IStructuralComparable.CompareTo(Object, IComparer) 하지 않습니다. 이 메서드는 배열의 각 요소에 Array.Sort(Array, IComparer) 대해 메서드에 의해 암시적으로 호출됩니다.

using System;
using System.Collections;
using System.Collections.Generic;

public class PitcherComparer<T1, T2, T3, T4> : IComparer
{
   public int Compare(object x, object y)
   {
      Tuple<T1, T2, T3, T4> tX = x as Tuple<T1, T2, T3, T4>;
      if (tX == null)
      { 
         return 0;
      }   
      else
      {
         Tuple<T1, T2, T3, T4> tY = y as Tuple<T1, T2, T3, T4>;
         return Comparer<T3>.Default.Compare(tX.Item3, tY.Item3);             
      }
   }
}

public class Example
{
   public static void Main()
   {
      Tuple<string, double, double, int>[] pitchers = 
                    { Tuple.Create("McHale, Joe", 240.1, 3.60, 221),
                      Tuple.Create("Paul, Dave", 233.1, 3.24, 231), 
                      Tuple.Create("Williams, Mike", 193.2, 4.00, 183),
                      Tuple.Create("Blair, Jack", 168.1, 3.48, 146), 
                      Tuple.Create("Henry, Walt", 140.1, 1.92, 96),
                      Tuple.Create("Lee, Adam", 137.2, 2.94, 109),
                      Tuple.Create("Rohr, Don", 101.0, 3.74, 110) };

      Console.WriteLine("The values in unsorted order:");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());

      Console.WriteLine();

      Array.Sort(pitchers, new PitcherComparer<string, double, double, int>());

      Console.WriteLine("The values sorted by earned run average (component 3):");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());
   }
}
// The example displays the following output;
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 3.6, 221)
//       (Paul, Dave, 233.1, 3.24, 231)
//       (Williams, Mike, 193.2, 4, 183)
//       (Blair, Jack, 168.1, 3.48, 146)
//       (Henry, Walt, 140.1, 1.92, 96)
//       (Lee, Adam, 137.2, 2.94, 109)
//       (Rohr, Don, 101, 3.74, 110)
//       
//       The values sorted by earned run average (component 3):
//       (Henry, Walt, 140.1, 1.92, 96)
//       (Lee, Adam, 137.2, 2.94, 109)
//       (Rohr, Don, 101, 3.74, 110)
//       (Blair, Jack, 168.1, 3.48, 146)
//       (McHale, Joe, 240.1, 3.6, 221)
//       (Paul, Dave, 233.1, 3.24, 231)
//       (Williams, Mike, 193.2, 4, 183)
open System
open System.Collections
open System.Collections.Generic

type PitcherComparer<'T1, 'T2, 'T3, 'T4>() =
    interface IComparer with
        member _.Compare(x: obj, y: obj) =
            match x with
            | :? Tuple<'T1, 'T2, 'T3, 'T4> as tX ->
                let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4>
                Comparer<'T3>.Default.Compare(tX.Item3, tY.Item3)             
            | _ -> 0

let pitchers = 
    [| Tuple.Create("McHale, Joe", 240.1, 3.60, 221)
       Tuple.Create("Paul, Dave", 233.1, 3.24, 231)
       Tuple.Create("Williams, Mike", 193.2, 4.00, 183)
       Tuple.Create("Blair, Jack", 168.1, 3.48, 146)
       Tuple.Create("Henry, Walt", 140.1, 1.92, 96)
       Tuple.Create("Lee, Adam", 137.2, 2.94, 109)
       Tuple.Create("Rohr, Don", 101.0, 3.74, 110) |]

printfn "The values in unsorted order:"
for pitcher in pitchers do
    printfn $"{pitcher}"

printfn ""

Array.Sort(pitchers, PitcherComparer<string, double, double, int>())

printfn "The values sorted by earned run average (component 3):"
for pitcher in pitchers do
    printfn $"{pitcher}"
// The example displays the following output
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 3.6, 221)
//       (Paul, Dave, 233.1, 3.24, 231)
//       (Williams, Mike, 193.2, 4, 183)
//       (Blair, Jack, 168.1, 3.48, 146)
//       (Henry, Walt, 140.1, 1.92, 96)
//       (Lee, Adam, 137.2, 2.94, 109)
//       (Rohr, Don, 101, 3.74, 110)
//       
//       The values sorted by earned run average (component 3):
//       (Henry, Walt, 140.1, 1.92, 96)
//       (Lee, Adam, 137.2, 2.94, 109)
//       (Rohr, Don, 101, 3.74, 110)
//       (Blair, Jack, 168.1, 3.48, 146)
//       (McHale, Joe, 240.1, 3.6, 221)
//       (Paul, Dave, 233.1, 3.24, 231)
//       (Williams, Mike, 193.2, 4, 183)
Imports System.Collections
Imports System.Collections.Generic

Public Class PitcherComparer(Of T1, T2, T3, T4) : Implements IComparer
   Public Function Compare(x As Object, y As Object) As Integer _
                   Implements IComparer.Compare
      Dim tX As Tuple(Of T1, T2, T3) = TryCast(x, Tuple(Of T1, T2, T3))
      If tX Is Nothing Then
         Return 0
      Else
         Dim tY As Tuple(Of T1, T2, T3) = DirectCast(y, Tuple(Of T1, T2, T3))
         Return Comparer(Of T3).Default.Compare(tx.Item3, tY.Item3)             
      End If
   End Function
End Class

Module Example
   Public Sub Main()
      Dim pitchers() = 
                { Tuple.Create("McHale, Joe", 240.1, 3.60, 221),
                  Tuple.Create("Paul, Dave", 233.1, 3.24, 231), 
                  Tuple.Create("Williams, Mike", 193.2, 4.00, 183),
                  Tuple.Create("Blair, Jack", 168.1, 3.48, 146), 
                  Tuple.Create("Henry, Walt", 140.1, 1.92, 96),
                  Tuple.Create("Lee, Adam", 137.2, 2.94, 109),
                  Tuple.Create("Rohr, Don", 101.0, 3.74, 110) }

      Console.WriteLine("The values in unsorted order:")
      For Each pitcher In pitchers
         Console.WriteLine(pitcher.ToString())
      Next
      Console.WriteLine()

      Array.Sort(pitchers, New PitcherComparer(Of String, Double, Double, Integer)())

      Console.WriteLine("The values sorted by earned run average (component 3):")
      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, 3.6, 221)
'       (Paul, Dave, 233.1, 3.24, 231)
'       (Williams, Mike, 193.2, 4, 183)
'       (Blair, Jack, 168.1, 3.48, 146)
'       (Henry, Walt, 140.1, 1.92, 96)
'       (Lee, Adam, 137.2, 2.94, 109)
'       (Rohr, Don, 101, 3.74, 110)
'       
'       The values sorted by earned run average (component 3):
'       (Henry, Walt, 140.1, 1.92, 96)
'       (Lee, Adam, 137.2, 2.94, 109)
'       (Rohr, Don, 101, 3.74, 110)
'       (Blair, Jack, 168.1, 3.48, 146)
'       (McHale, Joe, 240.1, 3.6, 221)
'       (Paul, Dave, 233.1, 3.24, 231)
'       (Williams, Mike, 193.2, 4, 183)

설명

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

이 메서드는 직접 호출할 수 있지만 컬렉션의 멤버를 정렬하는 매개 변수를 포함하는 IComparer 컬렉션 정렬 메서드에서 가장 일반적으로 호출됩니다. 예를 들어 생성자를 사용하여 SortedList.SortedList(IComparer) 인스턴스화되는 개체의 SortedList 메서드 및 Add 메서드에 의해 호출 Array.Sort(Array, IComparer) 됩니다.

주의

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

적용 대상