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

定义

使用指定的比较器将当前的 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> ,其中包含有关棒球投手的统计数据。 数据项包括投手的姓名、投球的局数、投手的平均得分 (投手每场比赛) 允许的平均运行次数,以及投手放弃的命中次数。 该示例按未排序顺序显示数组中每个元组的组件,对数组进行排序,然后调用 ToString 以按排序顺序显示每个元组的值。 为了对数组进行排序,该示例定义了一个实现 IComparer 接口的泛型PitcherComparer类,并按对象第三个组件的值( (获得的平均运行) 而不是第一个组件)对对象进行升序排序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) 方法。

适用于