Tuple<T1,T2,T3,T4>.IStructuralComparable.CompareTo 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用指定的比較子將目前的 Tuple<T1,T2,T3,T4> 物件和指定的物件進行比較,並且傳回一個整數,表示目前的物件在排序順序中位於指定之物件的前面、後面還是相的位置。
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,T4> 物件。
範例
下列範例會建立物件的陣列 Tuple<T1,T2,T3,T4> ,其中包含有關足球手的統計資料。 資料項目包括玩家名稱、推入的旅館數目、玩家所獲得的平均執行平均 (玩家允許每個遊戲的平均回合數) ,以及玩家已放棄的點擊次數。 此範例會以未排序的順序顯示陣列中每個 Tuple 的元件、排序陣列,然後呼叫 ToString 以依排序次序顯示每個 Tuple 的值。 為了排序陣列,此範例會定義泛型 PitcherComparer
類別,該類別會實 IComparer 作 介面,並以第三個元件的值遞增順序排序 Tuple<T1,T2,T3,T4> 物件, (所獲得的執行平均) 而不是第一個元件。 請注意,此範例不會直接呼叫 IStructuralComparable.CompareTo(Object, IComparer) 方法。 這個方法是由 Array.Sort(Array, IComparer) 陣列中每個元素的 方法隱含呼叫。
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)
備註
這個成員是明確介面成員實作, 只有在 Tuple<T1,T2,T3,T4> 執行個體轉換成 IStructuralComparable 介面時,才能使用這個成員。
雖然可以直接呼叫這個方法,但最常由集合排序方法呼叫,其中包含 IComparer 排序集合成員的參數。 例如,它會由 Array.Sort(Array, IComparer) 方法呼叫,以及 Add 使用 SortedList.SortedList(IComparer) 建構函式具現化之 SortedList 物件的 方法。
警告
方法 IStructuralComparable.CompareTo(Object, IComparer) 適用于排序作業。 當比較的主要用途是判斷兩個物件是否相等時,不應該使用它。 若要判斷兩個物件是否相等,請呼叫 IStructuralEquatable.Equals(Object, IEqualityComparer) 方法。