Tuple<T1,T2,T3,T4>.IStructuralComparable.CompareTo Metoda

Definicja

Porównuje bieżący Tuple<T1,T2,T3,T4> obiekt z określonym obiektem przy użyciu określonego porównania i zwraca liczbę całkowitą, która wskazuje, czy bieżący obiekt jest 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

Int32

Liczba całkowita ze znakiem 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 zgodne z .other

Implementuje

Wyjątki

other nie jest obiektem Tuple<T1,T2,T3,T4> .

Przykłady

Poniższy przykład tworzy tablicę Tuple<T1,T2,T3,T4> obiektów, które zawierają dane statystyczne dotyczące dzbanów baseballowych. Elementy danych obejmują nazwę dzbana, liczbę rozbić inningów, średnią przebiegu zarobionego dzbana (średnia liczba przebiegów dzban pozwala na grę) i liczba trafień dzban zrezygnował. W przykładzie pokazano składnik każdej krotki w tablicy w kolejności niesortowanej, sortuje tablicę, a następnie wywołuje ToString metodę wyświetlania wartości każdej krotki w kolejności sortowania. Aby posortować tablicę, przykład definiuje klasę ogólną PitcherComparer , która implementuje IComparer interfejs i sortuje Tuple<T1,T2,T3,T4> obiekty w kolejności rosnącej według wartości ich trzeciego składnika (średniej przebiegu uzyskanego) zamiast ich pierwszego składnika. Zwróć uwagę, że przykład nie wywołuje IStructuralComparable.CompareTo(Object, IComparer) bezpośrednio metody . Ta metoda jest wywoływana niejawnie przez metodę Array.Sort(Array, IComparer) dla każdego elementu w tablicy.

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)

Uwagi

Ten element jest jawną implementacją członków. Można go używać tylko wtedy, gdy Tuple<T1,T2,T3,T4> wystąpienie jest rzutowe do interfejsu IStructuralComparable .

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 uporządkowania 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 przy użyciu konstruktora SortedList.SortedList(IComparer) .

Przestroga

Metoda jest przeznaczona IStructuralComparable.CompareTo(Object, IComparer) 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(Object, IEqualityComparer) .

Dotyczy