Tuple<T1,T2>.IStructuralComparable.CompareTo(Object, IComparer) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した比較子を使用して、現在の Tuple<T1,T2> オブジェクトを指定したオブジェクトと比較し、現在のオブジェクトが並べ替え順序で指定したオブジェクトの前、後、または同じ位置にあるかどうかを示す整数を返します。
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に従います。
|
実装
例外
other は Tuple<T1,T2> オブジェクトではありません。
例
次の例では、学生の名前とテスト スコアで構成される Tuple<T1,T2> オブジェクトの配列を作成します。 配列内の各タプルのコンポーネントを並べ替えられていない順序で表示し、配列を並べ替え、 ToString を呼び出して、各タプルの値を並べ替えられた順序で表示します。 この例では、配列を並べ替えるために、ScoreComparer インターフェイスを実装するジェネリック IComparer クラスを定義し、Tuple<T1,T2> オブジェクトを最初のコンポーネントではなく 2 番目のコンポーネントの値で昇順に並べ替えます。 この例では、 IStructuralComparable.CompareTo メソッドは直接呼び出されないことに注意してください。 このメソッドは、配列内の各要素の Array.Sort(Array, IComparer) メソッドによって暗黙的に呼び出されます。
using System;
using System.Collections;
using System.Collections.Generic;
public class ScoreComparer<T1, T2> : IComparer
{
public int Compare(object x, object y)
{
Tuple<T1, T2> tX = x as Tuple<T1,T2>;
if (tX == null)
{
return 0;
}
else
{
Tuple<T1, T2> tY = y as Tuple<T1, T2>;
return Comparer<T2>.Default.Compare(tX.Item2, tY.Item2);
}
}
}
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 (var score in scores)
Console.WriteLine(score.ToString());
Console.WriteLine();
Array.Sort(scores, new ScoreComparer<string, Nullable<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)
// (Abbey, 92)
// (Dave, 88)
// (Sam, 91)
// (Ed, )
// (Penelope, 82)
// (Linda, 99)
// (Judith, 84)
//
// The values in sorted order:
// (Ed, )
// (Jack, 78)
// (Penelope, 82)
// (Judith, 84)
// (Dave, 88)
// (Sam, 91)
// (Abbey, 92)
// (Linda, 99)
open System
open System.Collections
open System.Collections.Generic
type ScoreComparer<'T1, 'T2>() =
interface IComparer with
member _.Compare(x: obj, y: obj) =
match x with
| :? Tuple<'T1, 'T2> as tX ->
let tY = y :?> Tuple<'T1, 'T2>
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2)
| _ -> 0
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, ScoreComparer<string, Nullable<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)
// (Abbey, 92)
// (Dave, 88)
// (Sam, 91)
// (Ed, )
// (Penelope, 82)
// (Linda, 99)
// (Judith, 84)
//
// The values in sorted order:
// (Ed, )
// (Jack, 78)
// (Penelope, 82)
// (Judith, 84)
// (Dave, 88)
// (Sam, 91)
// (Abbey, 92)
// (Linda, 99)
Imports System.Collections
Imports System.Collections.Generic
Public Class ScoreComparer(Of T1, T2) : Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Dim tX As Tuple(Of T1, T2) = TryCast(x, Tuple(Of T1, T2))
If tX Is Nothing Then
Return 0
Else
Dim tY As Tuple(Of T1, T2) = DirectCast(y, Tuple(Of T1, T2))
Return Comparer(Of T2).Default.Compare(tx.Item2, tY.Item2)
End If
End Function
End Class
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, New ScoreComparer(Of String, Nullable(Of 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)
' (Abbey, 92)
' (Dave, 88)
' (Sam, 91)
' (Ed, )
' (Penelope, 82)
' (Linda, 99)
' (Judith, 84)
'
' The values in sorted order:
' (Ed, )
' (Jack, 78)
' (Penelope, 82)
' (Judith, 84)
' (Dave, 88)
' (Sam, 91)
' (Abbey, 92)
' (Linda, 99)
注釈
このメンバーは、明示的なインターフェイス メンバーの実装です。 Tuple<T1,T2> インスタンスがIStructuralComparable インターフェイスにキャストされている場合にのみ使用できます。
このメソッドは直接呼び出すことができますが、最も一般的には、コレクションのメンバーを並べ替えるパラメーター IComparer 含むコレクション並べ替えメソッドによって呼び出されます。 たとえば、Array.Sort(Array, IComparer) コンストラクターを使用してインスタンス化されたAdd オブジェクトのSortedList メソッドとSortedList.SortedList(IComparer) メソッドによって呼び出されます。
Caution
IStructuralComparable.CompareTo メソッドは、並べ替え操作で使用することを目的としています。 比較の主な目的は、2 つのオブジェクトが等しいかどうかを判断する場合には使用しないでください。 2 つのオブジェクトが等しいかどうかを判断するには、 IStructuralEquatable.Equals メソッドを呼び出します。