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

Definice

Porovná aktuální Tuple<T1,T2> objekt se zadaným objektem pomocí zadaného porovnávače a vrátí celé číslo, které označuje, zda je aktuální objekt před, za nebo ve stejné pozici jako zadaný objekt v pořadí řazení.

 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

Objekt k porovnání s aktuální instancí.

comparer
IComparer

Objekt, který poskytuje vlastní pravidla pro porovnání.

Návraty

Int32

Celé číslo se znaménkem, které označuje relativní pozici této instance a other v pořadí řazení, jak je znázorněno v následující tabulce.

Hodnota Popis
Záporné celé číslo Tato instance předchází other.
Žádnou Tato instance a other má stejnou pozici v pořadí řazení.
Kladné celé číslo Tato instance následuje other.

Implementuje

Výjimky

other není Tuple<T1,T2> objekt.

Příklady

Následující příklad vytvoří pole Tuple<T1,T2> objektů, které se skládají z jména studenta a skóre testu. Zobrazí komponentu každé řazené kolekce členů v poli v neseřazeném pořadí, seřadí matici a potom volání ToString zobrazí hodnotu každé řazené kolekce členů v seřazeném pořadí. Chcete-li seřadit pole, příklad definuje obecnou ScoreComparer třídu, která implementuje IComparer rozhraní a seřadí Tuple<T1,T2> objekty vzestupně podle hodnoty jejich druhé komponenty, a nikoli podle jejich první komponenty. Všimněte si, že příklad přímo nevolá metodu IStructuralComparable.CompareTo . Tato metoda je volána implicitně metodou Array.Sort(Array, IComparer) pro každý prvek v poli.

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

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

public class Example
{
   public static void Main()
   {
      Tuple<string, Nullable<int>>[] scores = 
                    { new Tuple<string, Nullable<int>>("Jack", 78),
                      new Tuple<string, Nullable<int>>("Abbey", 92), 
                      new Tuple<string, Nullable<int>>("Dave", 88),
                      new Tuple<string, Nullable<int>>("Sam", 91), 
                      new Tuple<string, Nullable<int>>("Ed", null),
                      new Tuple<string, Nullable<int>>("Penelope", 82),
                      new Tuple<string, Nullable<int>>("Linda", 99),
                      new Tuple<string, Nullable<int>>("Judith", 84) };

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

      Console.WriteLine();

      Array.Sort(scores, new ScoreComparer<string, Nullable<int>>());

      Console.WriteLine("The values in sorted order:");
      foreach (var score in scores)
         Console.WriteLine(score.ToString());
   }
}
// The example displays the following output;
//       The values in unsorted order:
//       (Jack, 78)
//       (Abbey, 92)
//       (Dave, 88)
//       (Sam, 91)
//       (Ed, )
//       (Penelope, 82)
//       (Linda, 99)
//       (Judith, 84)
//       
//       The values in sorted order:
//       (Ed, )
//       (Jack, 78)
//       (Penelope, 82)
//       (Judith, 84)
//       (Dave, 88)
//       (Sam, 91)
//       (Abbey, 92)
//       (Linda, 99)
open System
open System.Collections
open System.Collections.Generic

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

let scores = 
    [| Tuple<string, Nullable<int>>("Jack", 78)
       Tuple<string, Nullable<int>>("Abbey", 92) 
       Tuple<string, Nullable<int>>("Dave", 88)
       Tuple<string, Nullable<int>>("Sam", 91) 
       Tuple<string, Nullable<int>>("Ed", Nullable())
       Tuple<string, Nullable<int>>("Penelope", 82)
       Tuple<string, Nullable<int>>("Linda", 99)
       Tuple<string, Nullable<int>>("Judith", 84) |]

printfn "The values in unsorted order:"
for score in scores do
    printfn $"{score}"

printfn ""

Array.Sort(scores, ScoreComparer<string, Nullable<int>>())

printfn "The values in sorted order:"
for score in scores do
    printfn $"{score}"
// The example displays the following output
//       The values in unsorted order:
//       (Jack, 78)
//       (Abbey, 92)
//       (Dave, 88)
//       (Sam, 91)
//       (Ed, )
//       (Penelope, 82)
//       (Linda, 99)
//       (Judith, 84)
//       
//       The values in sorted order:
//       (Ed, )
//       (Jack, 78)
//       (Penelope, 82)
//       (Judith, 84)
//       (Dave, 88)
//       (Sam, 91)
//       (Abbey, 92)
//       (Linda, 99)
Imports System.Collections
Imports System.Collections.Generic

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

Module Example
   Public Sub Main()
      Dim scores() As Tuple(Of String, Nullable(Of Integer)) = 
                      { New Tuple(Of String, Nullable(Of Integer))("Jack", 78),
                        New Tuple(Of String, Nullable(Of Integer))("Abbey", 92), 
                        New Tuple(Of String, Nullable(Of Integer))("Dave", 88),
                        New Tuple(Of String, Nullable(Of Integer))("Sam", 91), 
                        New Tuple(Of String, Nullable(Of Integer))("Ed", Nothing),
                        New Tuple(Of String, Nullable(Of Integer))("Penelope", 82),
                        New Tuple(Of String, Nullable(Of Integer))("Linda", 99),
                        New Tuple(Of String, Nullable(Of Integer))("Judith", 84) }

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

      Array.Sort(scores, New ScoreComparer(Of String, Nullable(Of Integer))())

      Console.WriteLine("The values in sorted order:")
      For Each score In scores
         Console.WriteLine(score.ToString())
      Next
   End Sub
End Module
' The example displays the following output;
'       The values in unsorted order:
'       (Jack, 78)
'       (Abbey, 92)
'       (Dave, 88)
'       (Sam, 91)
'       (Ed, )
'       (Penelope, 82)
'       (Linda, 99)
'       (Judith, 84)
'       
'       The values in sorted order:
'       (Ed, )
'       (Jack, 78)
'       (Penelope, 82)
'       (Judith, 84)
'       (Dave, 88)
'       (Sam, 91)
'       (Abbey, 92)
'       (Linda, 99)

Poznámky

Tento člen je explicitní implementace členu rozhraní. Lze ho použít pouze v Tuple<T1,T2> případě, že je instance přetypována do IStructuralComparable rozhraní.

I když lze tuto metodu volat přímo, je nejčastěji volána metodami řazení kolekce, které zahrnují IComparer parametry pro pořadí členů kolekce. Volá se například metodou Array.Sort(Array, IComparer) a Add metodou SortedList objektu, který je vytvořena pomocí konstruktoru SortedList.SortedList(IComparer) .

Upozornění

Metoda IStructuralComparable.CompareTo je určena k použití v operacích řazení. Není vhodné jej používat, pokud je primárním účelem porovnání zjištění, zda se dva objekty rovnají. Chcete-li zjistit, zda jsou dva objekty stejné, zavolejte metodu IStructuralEquatable.Equals .

Platí pro

Viz také