Tuple<T1,T2,T3,T4>.IComparable.CompareTo(Object) 方法

定義

將目前的 Tuple<T1,T2,T3,T4> 物件與指定的物件比較,並傳回可指出目前物件在排序次序中,是否在指定物件之前、之後或者相同之位置的整數。

 virtual int System.IComparable.CompareTo(System::Object ^ obj) = IComparable::CompareTo;
int IComparable.CompareTo (object obj);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo

參數

obj
Object

要與目前執行個體比較的物件。

傳回

帶正負號的整數,可指出此執行個體以及排序次序中 obj 的相對位置,如下表所示。

描述
負整數 這個執行個體位於 obj 之前。
這個執行個體和 obj 的排序位置相同。
正整數 這個執行個體位於 obj 之後。

實作

例外狀況

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

範例

下列範例會建立物件的陣列 Tuple<T1,T2,T3,T4> ,這些物件元件包含足球手的名稱、傾斜的旅館數目,以及已放棄的點擊次數和贏得的執行次數。 它會以未排序的順序顯示陣列中每個 Tuple 的元件、排序陣列,然後呼叫 ToString 以排序次序顯示每個 Tuple。 輸出顯示陣列已依名稱排序,這是第一個元件。 請注意,此範例不會直接呼叫 IComparable.CompareTo(Object) 方法。 這個方法是由 陣列中每個元素的 方法隱含 Sort(Array) 呼叫。

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      Tuple<string, decimal, int, int>[] pitchers  =  
                      { Tuple.Create("McHale, Joe", 240.1m, 221, 96),
                        Tuple.Create("Paul, Dave", 233.1m, 231, 84), 
                        Tuple.Create("Williams, Mike", 193.2m, 183, 86),
                        Tuple.Create("Blair, Jack", 168.1m, 146, 65), 
                        Tuple.Create("Henry, Walt", 140.1m, 96, 30),
                        Tuple.Create("Lee, Adam", 137.2m, 109, 45),
                        Tuple.Create("Rohr, Don", 101.0m, 110, 42) };

      // Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());
      Console.WriteLine();
      
      // Sort the array
      Array.Sort(pitchers);
      
      // Display the array in sorted order.
      Console.WriteLine("The values in sorted order:");
      foreach (var pitcher in pitchers)
         Console.WriteLine(pitcher.ToString());
   }
}
// The example displays the following output;
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Williams, Mike, 193.2, 183, 86)
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (Rohr, Don, 101, 110, 42)
//       
//       The values in sorted order:
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Rohr, Don, 101, 110, 42)
//       (Williams, Mike, 193.2, 183, 86)
open System

let pitchers  =  
    [| Tuple.Create("McHale, Joe", 240.1m, 221, 96)
       Tuple.Create("Paul, Dave", 233.1m, 231, 84)
       Tuple.Create("Williams, Mike", 193.2m, 183, 86)
       Tuple.Create("Blair, Jack", 168.1m, 146, 65)
       Tuple.Create("Henry, Walt", 140.1m, 96, 30)
       Tuple.Create("Lee, Adam", 137.2m, 109, 45)
       Tuple.Create("Rohr, Don", 101.0m, 110, 42) |]

// Display the array in unsorted order.
printfn "The values in unsorted order:"
for pitcher in pitchers do
    printfn $"{pitcher}"
printfn ""

// Sort the array
Array.Sort pitchers

// Display the array in sorted order.
printfn "The values in sorted order:"
for pitcher in pitchers do
    printfn $"{pitcher}"
// The example displays the following output
//       The values in unsorted order:
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Williams, Mike, 193.2, 183, 86)
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (Rohr, Don, 101, 110, 42)
//       
//       The values in sorted order:
//       (Blair, Jack, 168.1, 146, 65)
//       (Henry, Walt, 140.1, 96, 30)
//       (Lee, Adam, 137.2, 109, 45)
//       (McHale, Joe, 240.1, 221, 96)
//       (Paul, Dave, 233.1, 231, 84)
//       (Rohr, Don, 101, 110, 42)
//       (Williams, Mike, 193.2, 183, 86)
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim pitchers() =  
                { Tuple.Create("McHale, Joe", 240.1d, 221, 96),
                  Tuple.Create("Paul, Dave", 233.1d, 231, 84), 
                  Tuple.Create("Williams, Mike", 193.2d, 183, 86),
                  Tuple.Create("Blair, Jack", 168.1d, 146, 65), 
                  Tuple.Create("Henry, Walt", 140.1d, 96, 30),
                  Tuple.Create("Lee, Adam", 137.2d, 109, 45),
                  Tuple.Create("Rohr, Don", 101.0d, 110, 42) }

      ' Display the array in unsorted order.
      Console.WriteLine("The values in unsorted order:")
      For Each pitcher In pitchers
         Console.WriteLine(pitcher.ToString())
      Next
      Console.WriteLine()
      
      ' Sort the array
      Array.Sort(pitchers)
      
      ' Display the array in sorted order.
      Console.WriteLine("The values in sorted order:")
      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, 221, 96)
'       (Paul, Dave, 233.1, 231, 84)
'       (Williams, Mike, 193.2, 183, 86)
'       (Blair, Jack, 168.1, 146, 65)
'       (Henry, Walt, 140.1, 96, 30)
'       (Lee, Adam, 137.2, 109, 45)
'       (Rohr, Don, 101, 110, 42)
'       
'       The values in sorted order:
'       (Blair, Jack, 168.1, 146, 65)
'       (Henry, Walt, 140.1, 96, 30)
'       (Lee, Adam, 137.2, 109, 45)
'       (McHale, Joe, 240.1, 221, 96)
'       (Paul, Dave, 233.1, 231, 84)
'       (Rohr, Don, 101, 110, 42)
'       (Williams, Mike, 193.2, 183, 86)

備註

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

這個方法提供 IComparable.CompareTo 類別的實作 Tuple<T1,T2,T3,T4> 。 雖然可以直接呼叫 方法,但最常由集合排序方法的預設多載呼叫,例如 Array.Sort(Array)SortedList.Add ,以排序集合的成員。

警告

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

方法 IComparable.CompareTo 會使用預設物件比較子來比較每個元件。

適用於

另請參閱