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

現在のインスタンスと比較するオブジェクト。

戻り値

Int32

並べ替え順序における、このインスタンスと 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コレクションの並べ替えメソッドの既定のオーバーロードによって呼び出されます。

注意事項

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

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

適用対象

こちらもご覧ください