Tuple<T1>.IStructuralComparable.CompareTo(Object, IComparer) Metoda

Definicja

Porównuje bieżący Tuple<T1> obiekt z określonym obiektem przy użyciu określonego porównania i zwraca liczbę całkowitą wskazującą, czy bieżący obiekt znajduje się przed, po, czy w tej samej pozycji co określony obiekt w kolejności sortowania.

 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

Parametry

other
Object

Obiekt, który ma zostać porównany z bieżącym wystąpieniem.

comparer
IComparer

Obiekt dostarczający niestandardowe reguły na potrzeby porównania.

Zwraca

Podpisana liczba całkowita wskazująca względną pozycję tego wystąpienia i other w kolejności sortowania, jak pokazano w poniższej tabeli.

Wartość Opis
Ujemna liczba całkowita To wystąpienie poprzedza other.
Zero To wystąpienie i other mają tę samą pozycję w kolejności sortowania.
Dodatnia liczba całkowita To wystąpienie jest następujące: other.

Implementuje

Wyjątki

other nie jest obiektem Tuple<T1> .

Przykłady

W poniższym przykładzie zdefiniowano klasę ogólną o nazwie DescendingComparer , która implementuje IComparer<T> interfejs. DescendingComparer sortuje obiekty w kolejności malejącej, a nie rosnącej, odwracając wartość zwracaną przez domyślny porównujący dla określonego typu. Wystąpienie klasy ogólnej DescendingComparer jest następnie przekazywane do Array.Sort(Array, IComparer) metody w celu sortowania tablicy Tuple<T1> obiektów w kolejności malejącej. Należy pamiętać, że przykład nie wywołuje IStructuralComparable.CompareTo metody bezpośrednio. Ta metoda jest wywoływana niejawnie przez metodę Array.Sort(Array, IComparer) dla każdego elementu w tablicy.

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

Uwagi

Chociaż ta metoda może być wywoływana bezpośrednio, jest ona najczęściej wywoływana przez metody sortowania kolekcji, które zawierają IComparer parametry w celu porządkowenia elementów członkowskich kolekcji. Na przykład jest wywoływana przez metodę Array.Sort(Array, IComparer) i Add metodę SortedList obiektu, który jest tworzone za pomocą konstruktora SortedList.SortedList(IComparer) .

Przestroga

Metoda jest przeznaczona IStructuralComparable.CompareTo do użycia w operacjach sortowania. Nie należy jej używać, gdy głównym celem porównania jest ustalenie, czy dwa obiekty są sobie równe. Aby określić, czy dwa obiekty są równe, wywołaj metodę IStructuralEquatable.Equals .

Dotyczy

Zobacz też