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

Definisi

Membandingkan objek saat ini Tuple<T1,T2,T3,T4> dengan objek tertentu dengan menggunakan perbandingan tertentu dan mengembalikan bilangan bulat yang menunjukkan apakah objek saat ini adalah sebelum, sesudah, atau dalam posisi yang sama dengan objek yang ditentukan dalam urutan pengurutan.

 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

Parameter

other
Object

Objek untuk dibandingkan dengan instans saat ini.

comparer
IComparer

Objek yang menyediakan aturan kustom untuk perbandingan.

Mengembalikan

Bilangan bulat bertanda tangan yang menunjukkan posisi relatif instans ini dan other dalam urutan pengurutan, seperti yang ditunjukkan dalam tabel berikut.

Nilai Deskripsi
Bilangan bulat negatif Instans ini mendahului other.
Nol Instans ini dan other memiliki posisi yang sama dalam urutan pengurutan.
Bilangan bulat positif Instans otherini mengikuti .

Penerapan

Pengecualian

Contoh

Contoh berikut membuat array Tuple<T1,T2,T3,T4> objek yang berisi data statistik tentang pitcher baseball. Item data termasuk nama pitcher, jumlah babak yang dilemparkan, rata-rata eksekusi yang diperoleh pitcher (jumlah rata-rata eksekusi yang diizinkan pelempar per game), dan jumlah temuan pitcher telah menyerah. Contoh menampilkan komponen setiap tuple dalam array dalam urutan yang tidak diurutkan, mengurutkan array, lalu memanggil ToString untuk menampilkan nilai setiap tuple dalam urutan yang diurutkan. Untuk mengurutkan array, contoh menentukan kelas generik PitcherComparer yang mengimplementasikan IComparer antarmuka dan mengurutkan Tuple<T1,T2,T3,T4> objek dalam urutan naik berdasarkan nilai komponen ketiganya (rata-rata eksekusi yang didapatkan) daripada komponen pertamanya. Perhatikan bahwa contoh tidak secara langsung memanggil IStructuralComparable.CompareTo(Object, IComparer) metode . Metode ini dipanggil secara implisit oleh Array.Sort(Array, IComparer) metode untuk setiap elemen dalam array.

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)

Keterangan

Anggota ini adalah implementasi anggota antarmuka eksplisit. Ini hanya dapat digunakan ketika instans dilemparkan Tuple<T1,T2,T3,T4> ke IStructuralComparable antarmuka.

Meskipun metode ini dapat dipanggil secara langsung, metode ini paling umum disebut dengan metode pengurutan koleksi yang menyertakan IComparer parameter untuk mengurutkan anggota koleksi. Misalnya, ini dipanggil dengan Array.Sort(Array, IComparer) metode dan Add metode SortedList objek yang diinstruksikan dengan menggunakan SortedList.SortedList(IComparer) konstruktor.

Perhatian

Metode IStructuralComparable.CompareTo(Object, IComparer) ini ditujukan untuk digunakan dalam operasi pengurutan. Ini tidak boleh digunakan ketika tujuan utama perbandingan adalah untuk menentukan apakah dua objek sama. Untuk menentukan apakah dua objek sama, panggil IStructuralEquatable.Equals(Object, IEqualityComparer) metode .

Berlaku untuk