語言

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

定義

將當前 Tuple<T1,T2> 物件與指定物件比較,並回傳一個整數,表示該物件在排序順序中是否位於指定物件之前、之後或相同位置。

 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> 物件。

範例

以下範例建立一個由學生姓名和考試分數組成的物件陣列 Tuple<T1,T2> 。 它會以未排序順序顯示陣列中每個元組的組件,然後對陣列進行排序,然後呼叫 ToString 以排序順序顯示每個元組的值。 輸出顯示陣列已依其第一個組件排序。 請注意,範例中並未直接呼叫該 IComparable.CompareTo(Object) 方法。 此方法由 方法 Sort(Array) 隱含呼叫,針對陣列中的每個元素。

using System;

public class Example
{
   public static void Main()
   {
      Tuple<string, Nullable<int>>[] scores = 
                    { new Tuple<string, Nullable<int>>("Jack", 78),
                      new Tuple<string, Nullable<int>>("Abbey", 92), 
                      new Tuple<string, Nullable<int>>("Dave", 88),
                      new Tuple<string, Nullable<int>>("Sam", 91), 
                      new Tuple<string, Nullable<int>>("Ed", null),
                      new Tuple<string, Nullable<int>>("Penelope", 82),
                      new Tuple<string, Nullable<int>>("Linda", 99),
                      new Tuple<string, Nullable<int>>("Judith", 84) };

      Console.WriteLine("The values in unsorted order:");
      foreach (Tuple<string, Nullable<int>> score in scores)
         Console.WriteLine(score.ToString());

      Console.WriteLine();

      Array.Sort(scores);

      Console.WriteLine("The values in sorted order:");
      foreach (Tuple<string, Nullable<int>> score in scores)
         Console.WriteLine(score.ToString());
   }
}
// The example displays the following output;
//       The values in unsorted order:
//       (Jack, 78)
//       (Abbey, 92)
//       (Dave, 88)
//       (Sam, 91)
//       (Ed, )
//       (Penelope, 82)
//       (Linda, 99)
//       (Judith, 84)
//       
//       The values in sorted order:
//       (Abbey, 92)
//       (Dave, 88)
//       (Ed, )
//       (Jack, 78)
//       (Judith, 84)
//       (Linda, 99)
//       (Penelope, 82)
//       (Sam, 91)
open System

let scores = 
    [| Tuple<string, Nullable<int>>("Jack", 78)
       Tuple<string, Nullable<int>>("Abbey", 92) 
       Tuple<string, Nullable<int>>("Dave", 88)
       Tuple<string, Nullable<int>>("Sam", 91) 
       Tuple<string, Nullable<int>>("Ed", Nullable())
       Tuple<string, Nullable<int>>("Penelope", 82)
       Tuple<string, Nullable<int>>("Linda", 99)
       Tuple<string, Nullable<int>>("Judith", 84) |]

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

printfn ""

Array.Sort scores

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)
//       (Abbey, 92)
//       (Dave, 88)
//       (Sam, 91)
//       (Ed, )
//       (Penelope, 82)
//       (Linda, 99)
//       (Judith, 84)
//       
//       The values in sorted order:
//       (Abbey, 92)
//       (Dave, 88)
//       (Ed, )
//       (Jack, 78)
//       (Judith, 84)
//       (Linda, 99)
//       (Penelope, 82)
//       (Sam, 91)
Module Example
   Public Sub Main()
      Dim scores() As Tuple(Of String, Nullable(Of Integer)) = 
                      { New Tuple(Of String, Nullable(Of Integer))("Jack", 78),
                        New Tuple(Of String, Nullable(Of Integer))("Abbey", 92), 
                        New Tuple(Of String, Nullable(Of Integer))("Dave", 88),
                        New Tuple(Of String, Nullable(Of Integer))("Sam", 91), 
                        New Tuple(Of String, Nullable(Of Integer))("Ed", Nothing),
                        New Tuple(Of String, Nullable(Of Integer))("Penelope", 82),
                        New Tuple(Of String, Nullable(Of Integer))("Linda", 99),
                        New Tuple(Of String, Nullable(Of Integer))("Judith", 84) }

      Console.WriteLine("The values in unsorted order:")
      For Each score In scores
         Console.WriteLine(score.ToString())
      Next
      Console.WriteLine()

      Array.Sort(scores)

      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)
'       (Abbey, 92)
'       (Dave, 88)
'       (Sam, 91)
'       (Ed, )
'       (Penelope, 82)
'       (Linda, 99)
'       (Judith, 84)
'       
'       The values in sorted order:
'       (Abbey, 92)
'       (Dave, 88)
'       (Ed, )
'       (Jack, 78)
'       (Judith, 84)
'       (Linda, 99)
'       (Penelope, 82)
'       (Sam, 91)

備註

此成員為明確介面成員實作。 它只能在實 Tuple<T1,T2> 例投射到 IComparable 介面時使用。

此方法提供 IComparable.CompareTo 類別的實作 Tuple<T1,T2> 。 雖然此方法可直接呼叫,但通常由預設的集合排序方法(如 Array.Sort(Array) 和 SortedList.Add)來排序集合成員。

注意事項

此 Tuple<T1,T2>.IComparable.CompareTo 方法主要用於排序操作。 當比較的主要目的是判斷兩個物體是否相等時,不應使用此方法。 要判斷兩個物體是否相等,請呼叫該 Equals 方法。

此 Tuple<T1,T2>.IComparable.CompareTo 方法使用預設的物件比較器來比較每個元件。

適用於

另請參閱