共用方式為


Tuple<T1,T2,T3>.IStructuralComparable.CompareTo 方法

定義

使用指定的比較子將目前的 Tuple<T1,T2,T3> 物件和指定的物件進行比較,並且傳回一個整數,表示目前的物件在排序順序中位於指定之物件的前面、後面還是相的位置。

 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 之前。
這個執行個體和 other 的排序位置相同。
正整數 這個執行個體位於 other 之後。

實作

例外狀況

other 不是 Tuple<T1,T2,T3> 物件。

範例

下列範例會建立物件陣列 Tuple<T1,T2,T3> ,其中包含學生的名稱、平均測試分數和測試數目。 它會以未排序的順序顯示陣列中每個 Tuple 的元件、排序陣列,然後呼叫 ToString 以排序次序顯示每個 Tuple 的值。 為了排序陣列,此範例會定義泛型類別,這個類別 ScoreComparerIComparer 實作 介面,並以第二個元件的值,而不是第一個元件的值,以遞增順序排序 Tuple<T1,T2,T3> 物件。 請注意,此範例不會直接呼叫 Tuple<T1,T2,T3>.IStructuralComparable.CompareTo 方法。 這個方法是由 陣列中每個元素的 方法隱含 Array.Sort(Array, IComparer) 呼叫。

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

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

public class Example
{
   public static void Main()
   {
      Tuple<string, double, int>[] scores = 
                { Tuple.Create("Jack", 78.8, 8),
                  Tuple.Create("Abbey", 92.1, 9), 
                  Tuple.Create("Dave", 88.3, 9),
                  Tuple.Create("Sam", 91.7, 8), 
                  Tuple.Create("Ed", 71.2, 5),
                  Tuple.Create("Penelope", 82.9, 8),
                  Tuple.Create("Linda", 99.0, 9),
                  Tuple.Create("Judith", 84.3, 9) };

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

      Console.WriteLine();

      Array.Sort(scores, new ScoreComparer<string, double, 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.8, 8)
//       (Abbey, 92.1, 9)
//       (Dave, 88.3, 9)
//       (Sam, 91.7, 8)
//       (Ed, 71.2, 5)
//       (Penelope, 82.9, 8)
//       (Linda, 99, 9)
//       (Judith, 84.3, 9)
//       
//       The values in sorted order:
//       (Ed, 71.2, 5)
//       (Jack, 78.8, 8)
//       (Penelope, 82.9, 8)
//       (Judith, 84.3, 9)
//       (Dave, 88.3, 9)
//       (Sam, 91.7, 8)
//       (Abbey, 92.1, 9)
//       (Linda, 99, 9)
open System
open System.Collections
open System.Collections.Generic

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

let scores = 
    [| Tuple.Create("Jack", 78.8, 8)
       Tuple.Create("Abbey", 92.1, 9) 
       Tuple.Create("Dave", 88.3, 9)
       Tuple.Create("Sam", 91.7, 8) 
       Tuple.Create("Ed", 71.2, 5)
       Tuple.Create("Penelope", 82.9, 8)
       Tuple.Create("Linda", 99.0, 9)
       Tuple.Create("Judith", 84.3, 9) |]

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

printfn ""

Array.Sort(scores, ScoreComparer<string, double, 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.8, 8)
//       (Abbey, 92.1, 9)
//       (Dave, 88.3, 9)
//       (Sam, 91.7, 8)
//       (Ed, 71.2, 5)
//       (Penelope, 82.9, 8)
//       (Linda, 99, 9)
//       (Judith, 84.3, 9)
//       
//       The values in sorted order:
//       (Ed, 71.2, 5)
//       (Jack, 78.8, 8)
//       (Penelope, 82.9, 8)
//       (Judith, 84.3, 9)
//       (Dave, 88.3, 9)
//       (Sam, 91.7, 8)
//       (Abbey, 92.1, 9)
//       (Linda, 99, 9)
Imports System.Collections
Imports System.Collections.Generic

Public Class ScoreComparer(Of T1, T2, T3) : 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 T2).Default.Compare(tx.Item2, tY.Item2)             
      End If
   End Function
End Class

Module Example
   Public Sub Main()
      Dim scores() = 
                 { Tuple.Create("Jack", 78.8, 8),
                   Tuple.Create("Abbey", 92.1, 9), 
                   Tuple.Create("Dave", 88.3, 9),
                   Tuple.Create("Sam", 91.7, 8), 
                   Tuple.Create("Ed", 71.2, 5),
                   Tuple.Create("Penelope", 82.9, 8),
                   Tuple.Create("Linda", 99.0, 9),
                   Tuple.Create("Judith", 84.3, 9) }

      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, Double, 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.8, 8)
'       (Abbey, 92.1, 9)
'       (Dave, 88.3, 9)
'       (Sam, 91.7, 8)
'       (Ed, 71.2, 5)
'       (Penelope, 82.9, 8)
'       (Linda, 99, 9)
'       (Judith, 84.3, 9)
'       
'       The values in sorted order:
'       (Ed, 71.2, 5)
'       (Jack, 78.8, 8)
'       (Penelope, 82.9, 8)
'       (Judith, 84.3, 9)
'       (Dave, 88.3, 9)
'       (Sam, 91.7, 8)
'       (Abbey, 92.1, 9)
'       (Linda, 99, 9)

備註

這個成員是明確介面成員實作, 只有在 Tuple<T1,T2,T3> 執行個體轉換成 IStructuralComparable 介面時,才能使用這個成員。

雖然可以直接呼叫這個方法,但最常由集合排序方法呼叫,其中包含 IComparer 排序集合成員的參數。 例如,它是由 Array.Sort(Array, IComparer) 方法呼叫,以及 Add 使用 SortedList.SortedList(IComparer) 建構函式具現化之 物件的 方法 SortedList

警告

方法 Tuple<T1,T2,T3>.IStructuralComparable.CompareTo 適用于排序作業。 當比較的主要用途是判斷兩個物件是否相等時,就不應該使用它。 若要判斷兩個物件是否相等,請呼叫 Tuple<T1,T2,T3>.IStructuralEquatable.Equals 方法。

適用於

另請參閱