Tuple<T1,T2,T3>.IStructuralComparable.CompareTo メソッド

定義

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

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

実装

例外

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

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

using System;
using System.Collections;
using System.Collections.Generic;

public class ScoreComparer<T1, T2, T3> : IComparer
{
   public int Compare(object x, object y)
   {
      Tuple<T1, T2, T3> tX = x as Tuple<T1,T2, T3>;
      if (tX == null)
      { 
         return 0;
      }   
      else
      {
         Tuple<T1, T2, T3> tY = y as Tuple<T1, T2, T3>;
         return Comparer<T2>.Default.Compare(tX.Item2, tY.Item2);             
      }
   }
}

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, new ScoreComparer<string, double, int>());

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

type ScoreComparer<'T1, 'T2, 'T3>() =
    interface IComparer with
        member _.Compare(x: obj, y: obj) =
            match x with
            | :? Tuple<'T1, 'T2, 'T3> as tX -> 
                let tY = y :?> Tuple<'T1, 'T2, 'T3>
                Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2)             
            | _ -> 0

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, ScoreComparer<string, double, int>())

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

Public Class ScoreComparer(Of T1, T2, T3) : 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 T2).Default.Compare(tx.Item2, tY.Item2)             
      End If
   End Function
End Class

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, New ScoreComparer(Of String, Double, Integer)())

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

注釈

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

このメソッドは直接呼び出すことができますが、最も一般的には、コレクションのメンバーを並べ替えるパラメーター IComparer 含むコレクション並べ替えメソッドによって呼び出されます。 たとえば、Array.Sort(Array, IComparer) コンストラクターを使用してインスタンス化されたAdd オブジェクトのSortedList メソッドとSortedList.SortedList(IComparer) メソッドによって呼び出されます。

Caution

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

適用対象

こちらもご覧ください