Tuple<T1>.IStructuralComparable.CompareTo(Object, IComparer) 메서드

정의

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

 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> 개체가 아닙니다.

예제

다음 예제에서는 인터페이스를 구현하는 라는 DescendingComparer 제네릭 클래스를 IComparer<T> 정의합니다. DescendingComparer 는 특정 형식에 대한 기본 비교자가 반환하는 값을 반대로 하여 오름차순이 아닌 내림차순으로 개체를 정렬합니다. 그런 다음 제네릭 DescendingComparer 클래스의 인스턴스가 메서드에 Array.Sort(Array, IComparer) 전달되어 개체 배열 Tuple<T1> 을 내림차순으로 정렬합니다. 예제에서는 메서드를 직접 호출 IStructuralComparable.CompareTo 하지 않습니다. 이 메서드는 배열의 각 요소에 Array.Sort(Array, IComparer) 대해 메서드에 의해 암시적으로 호출됩니다.

using System;
using System.Collections.Generic;

public class DescendingComparer<T> : IComparer<T>
{
    public int Compare(T x, T y) 
    {
        return -1 * Comparer<T>.Default.Compare(x, y);
    }
}

class CompareTo2
{
   static void Main()
   {
       Tuple<Double>[] values = { Tuple.Create(13.54),
                                  Tuple.Create(Double.NaN),
                                  Tuple.Create(-189.42993),
                                  Tuple.Create(Double.PositiveInfinity),
                                  Tuple.Create(Double.Epsilon),
                                  Tuple.Create(1.934E-17),
                                  Tuple.Create(Double.NegativeInfinity),
                                  Tuple.Create(-0.000000000003588),
                                  null };
       Console.WriteLine("The values in unsorted order:");
       foreach (var value in values)
           if (value != null)
               Console.WriteLine("   {0}", value.Item1);
           else
               Console.WriteLine("   <null>");
       Console.WriteLine();

       Array.Sort(values, new DescendingComparer<Tuple<Double>>());

       Console.WriteLine("The values sorted in descending order:");
       foreach (var value in values)
           if (value != null)
               Console.WriteLine("   {0}", value.Item1);
           else
               Console.WriteLine("   <null>");
    }
}
// The example displays the following output:
//      The values in unsorted order:
//         13.54
//         NaN
//         -189.42993
//         Infinity
//         4.94065645841247E-324
//         1.934E-17
//         -Infinity
//         -3.588E-12
//
//      The values sorted in descending order:
//         Infinity
//         13.54
//         1.934E-17
//         4.94065645841247E-324
//         -3.588E-12
//         -189.42993
//         -Infinity
//         NaN
open System
open System.Collections.Generic

type DescendingComparer<'T>() =
    interface IComparer<'T> with
        member _.Compare(x: 'T, y: 'T) = 
            -1 * Comparer<'T>.Default.Compare(x, y)

let values = 
    [| Tuple.Create 13.54
       Tuple.Create Double.NaN
       Tuple.Create -189.42993
       Tuple.Create Double.PositiveInfinity
       Tuple.Create Double.Epsilon
       Tuple.Create 1.934E-17
       Tuple.Create Double.NegativeInfinity
       Tuple.Create -0.000000000003588
       null |]
printfn "The values in unsorted order:"
for value in values do
    printfn $"   %A{value.Item1}"
printfn ""

Array.Sort(values, DescendingComparer<Tuple<Double>>())

printfn "The values sorted in descending order:"
for value in values do
    printfn $"   %A{value.Item1}"
// The example displays the following output:
//      The values in unsorted order:
//         13.54
//         NaN
//         -189.42993
//         Infinity
//         4.94065645841247E-324
//         1.934E-17
//         -Infinity
//         -3.588E-12
//
//      The values sorted in descending order:
//         Infinity
//         13.54
//         1.934E-17
//         4.94065645841247E-324
//         -3.588E-12
//         -189.42993
//         -Infinity
//         NaN
Imports System.Collections.Generic

Public Class DescendingComparer(Of T) : Implements IComparer(Of T)
    Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements IComparer(Of T).Compare
        Return -1 * Comparer(Of T).Default.Compare(x, y)
    End Function
End Class

Module Example
    Sub Main()
        Dim values() = { Tuple.Create(13.54),
                         Tuple.Create(Double.NaN),
                         Tuple.Create(-189.42993),
                         Tuple.Create(Double.PositiveInfinity),
                         Tuple.Create(Double.Epsilon),
                         Tuple.Create(1.934E-17),
                         Tuple.Create(Double.NegativeInfinity),
                         Tuple.Create(-0.000000000003588),
                         Nothing}

        Console.WriteLine("The values in unsorted order:")
        For Each value As Tuple(Of Double) In values
            If value IsNot Nothing Then
                Console.WriteLine("   {0}", value.Item1)
            Else
                Console.WriteLine("   <null>")
            End If
        Next
        Console.WriteLine()

        Array.Sort(values, New DescendingComparer(Of Tuple(Of Double)))

        Console.WriteLine("The values sorted in descending order:")
        For Each value As Tuple(Of Double) In values
            If value IsNot Nothing Then
                Console.WriteLine("   {0}", value.Item1)
            Else
                Console.WriteLine("   <null>")
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'      The values in unsorted order:
'         13.54
'         NaN
'         -189.42993
'         Infinity
'         4.94065645841247E-324
'         1.934E-17
'         -Infinity
'         -3.588E-12
'
'      The values sorted in descending order:
'         Infinity
'         13.54
'         1.934E-17
'         4.94065645841247E-324
'         -3.588E-12
'         -189.42993
'         -Infinity
'         NaN

설명

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

주의

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

적용 대상

추가 정보