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

정의

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

 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

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

반환

Int32

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

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

구현

예외

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

예제

다음 예제에서는 미국 프로 축구에서 러닝 백에 대 한 경력 통계 데이터를 포함 하는 개체의 Tuple<T1,T2,T3,T4,T5> 배열을 만듭니다. 5 튜플의 구성 요소는 플레이어의 이름, 그가 플레이한 게임 수, 운반 또는 시도 횟수, 얻은 총 야드 수 및 득점한 터치다운 수로 구성됩니다. 이 예제에서는 배열의 각 튜플 구성 요소를 정렬되지 않은 순서로 표시하고 배열을 정렬한 다음 각 튜플을 정렬된 순서로 표시하도록 호출 ToString 합니다. 배열을 정렬하기 위해 이 예제에서는 인터페이스를 구현 IComparer 하는 제네릭 YardsGained 클래스를 정의하고 첫 번째 구성 요소가 아닌 네 번째 구성 요소(야드 획득)의 값을 기준으로 개체를 내림차순으로 정렬 Tuple<T1,T2,T3,T4,T5> 합니다. 이 예제에서는 메서드를 직접 호출 IStructuralComparable.CompareTo 하지 않습니다. 이 메서드는 배열의 각 요소에 Array.Sort(Array, IComparer) 대해 메서드에 의해 암시적으로 호출됩니다.

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

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

public class Example
{
   public static void Main()
   {
      // Organization of runningBacks 5-tuple:
      //    Component 1: Player name
      //    Component 2: Number of games played
      //    Component 3: Number of attempts (carries)
      //    Component 4: Number of yards gained 
      //    Component 5: Number of touchdowns   
      Tuple<string, int, int, int, int>[] runningBacks =
           { Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),  
             Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),            
             Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),            
             Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),            
             Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) }; 

      // Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:");
      foreach (var runningBack in runningBacks)
         Console.WriteLine(runningBack.ToString());
      Console.WriteLine();
      
      // Sort the array
      Array.Sort(runningBacks, new YardsGained<string, int, int, int, int>());
      
      // Display the array in sorted order.
      Console.WriteLine("The values in sorted order:");
      foreach (var runningBack in runningBacks)
         Console.WriteLine(runningBack.ToString());
   }
}
// The example displays the following output:
//       The values in unsorted order:
//       (Payton, Walter, 190, 3838, 16726, 110)
//       (Sanders, Barry, 153, 3062, 15269, 99)
//       (Brown, Jim, 118, 2359, 12312, 106)
//       (Dickerson, Eric, 144, 2996, 13259, 90)
//       (Faulk, Marshall, 176, 2836, 12279, 100)
//       
//       The values in sorted order:
//       (Brown, Jim, 118, 2359, 12312, 106)
//       (Dickerson, Eric, 144, 2996, 13259, 90)
//       (Faulk, Marshall, 176, 2836, 12279, 100)
//       (Payton, Walter, 190, 3838, 16726, 110)
//       (Sanders, Barry, 153, 3062, 15269, 99)
open System
open System.Collections
open System.Collections.Generic

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

// Organization of runningBacks 5-tuple:
//    Component 1: Player name
//    Component 2: Number of games played
//    Component 3: Number of attempts (carries)
//    Component 4: Number of yards gained 
//    Component 5: Number of touchdowns   
let runningBacks =
    [| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
       Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
       Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
       Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
       Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]

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

// Sort the array
Array.Sort(runningBacks, YardsGained<string, int, int, int, int>())

// Display the array in sorted order.
printfn "The values in sorted order:"
for runningBack in runningBacks do
    printfn $"{runningBack}"
// The example displays the following output:
//       The values in unsorted order:
//       (Payton, Walter, 190, 3838, 16726, 110)
//       (Sanders, Barry, 153, 3062, 15269, 99)
//       (Brown, Jim, 118, 2359, 12312, 106)
//       (Dickerson, Eric, 144, 2996, 13259, 90)
//       (Faulk, Marshall, 176, 2836, 12279, 100)
//       
//       The values in sorted order:
//       (Brown, Jim, 118, 2359, 12312, 106)
//       (Dickerson, Eric, 144, 2996, 13259, 90)
//       (Faulk, Marshall, 176, 2836, 12279, 100)
//       (Payton, Walter, 190, 3838, 16726, 110)
//       (Sanders, Barry, 153, 3062, 15269, 99)
Imports System.Collections
Imports System.Collections.Generic

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

Module Example
   Public Sub Main()
      ' Organization of runningBacks 5-tuple:
      '    Component 1: Player name
      '    Component 2: Number of games played
      '    Component 3: Number of attempts (carries)
      '    Component 4: Number of yards gained 
      '    Component 5: Number of touchdowns   
      Dim runningBacks() =
          { Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),  
            Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),            
            Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),            
            Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),            
            Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) } 

      ' Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:")
      For Each runningBack In runningBacks
         Console.WriteLine(runningBack.ToString())
      Next
      Console.WriteLine()
      
      ' Sort the array
      Array.Sort(runningBacks, New YardsGained(Of String, Integer, Integer, Integer, Integer)())
      
      ' Display the array in sorted order.
      Console.WriteLine("The values in sorted order:")
      For Each runningBack In runningBacks
         Console.WriteLine(runningBack.ToString())
      Next
   End Sub
End Module
' The example displays the following output:
'       The values in unsorted order:
'       (Payton, Walter, 190, 3838, 16726, 110)
'       (Sanders, Barry, 153, 3062, 15269, 99)
'       (Brown, Jim, 118, 2359, 12312, 106)
'       (Dickerson, Eric, 144, 2996, 13259, 90)
'       (Faulk, Marshall, 176, 2836, 12279, 100)
'       
'       The values in sorted order:
'       (Payton, Walter, 190, 3838, 16726, 110)
'       (Sanders, Barry, 153, 3062, 15269, 99)
'       (Dickerson, Eric, 144, 2996, 13259, 90)
'       (Brown, Jim, 118, 2359, 12312, 106)
'       (Faulk, Marshall, 176, 2836, 12279, 100)

설명

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

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

주의

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

적용 대상