Tuple<T1,T2,T3>.IComparable.CompareTo(Object) メソッド

定義

現在の Tuple<T1,T2,T3> オブジェクトを指定したオブジェクトと比較し、現在のオブジェクトが並べ替え順序で指定したオブジェクトの前、後、または同じ位置にあるかどうかを示す整数を返します。

 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に従います。

実装

例外

objTuple<T1,T2,T3> オブジェクトではありません。

次の例では、学生の名前、平均テスト スコア、テスト数で構成されるコンポーネントを持つ Tuple<T1,T2,T3> オブジェクトの配列を作成します。 配列内の各タプルのコンポーネントが並べ替えられていない順序で表示され、配列が並べ替えられた後、 ToString を呼び出して、各タプルが並べ替えられた順序で表示されます。 出力は、配列が最初のコンポーネントで並べ替えられたことを示しています。 この例では、 Tuple<T1,T2,T3>.IComparable.CompareTo メソッドは直接呼び出されないことに注意してください。 このメソッドは、配列内の各要素の Sort(Array) メソッドによって暗黙的に呼び出されます。

using System;

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);

      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:
//    (Abbey, 92.1, 9)
//    (Dave, 88.3, 9)
//    (Ed, 71.2, 5)
//    (Jack, 78.8, 8)
//    (Judith, 84.3, 9)
//    (Linda, 99, 9)
//    (Penelope, 82.9, 8)
//    (Sam, 91.7, 8)
open System

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

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:
//    (Abbey, 92.1, 9)
//    (Dave, 88.3, 9)
//    (Ed, 71.2, 5)
//    (Jack, 78.8, 8)
//    (Judith, 84.3, 9)
//    (Linda, 99, 9)
//    (Penelope, 82.9, 8)
//    (Sam, 91.7, 8)
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)

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

注釈

このメンバーは、明示的なインターフェイス メンバーの実装です。 Tuple<T1,T2,T3> インスタンスがIComparable インターフェイスにキャストされている場合にのみ使用できます。

このメソッドは、IComparable.CompareTo クラスのTuple<T1,T2,T3>実装を提供します。 メソッドは直接呼び出すことができますが、最も一般的には、コレクションのメンバーを並べ替えるために、 Array.Sort(Array)SortedList.Addなどのコレクション並べ替えメソッドの既定のオーバーロードによって呼び出されます。

Caution

Tuple<T1,T2,T3>.IComparable.CompareTo メソッドは、並べ替え操作で使用することを目的としています。 比較の主な目的は、2 つのオブジェクトが等しいかどうかを判断する場合には使用しないでください。 2 つのオブジェクトが等しいかどうかを判断するには、 Equals メソッドを呼び出します。

Tuple<T1,T2,T3>.IComparable.CompareToメソッドは、既定のオブジェクト比較子を使用して各コンポーネントを比較します。

適用対象

こちらもご覧ください