Tuple.IStructuralComparable.CompareTo(Object, IComparer) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Compares the current Tuple
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
Parameters
- other
- Object
An object to compare with the current instance.
- comparer
- IComparer
An object that provides custom rules for comparison.
Returns
A signed integer that indicates the relative position of this instance and other in the sort order, as shown in the following table.
| Value | Description |
|---|---|
| A negative integer | This instance precedes other.
|
| Zero | This instance and other have the same position in the sort order.
|
| A positive integer | This instance follows other.
|
Implements
Exceptions
other is not a Tuple
Examples
The following example creates an array of TupleScoreComparer class that implements the IComparer interface and sorts the Tuple
using System;
using System.Collections;
using System.Collections.Generic;
public class ScoreComparer : IComparer
{
public int Compare(object x, object y)
{
Tuple tX = x as Tuple;
if (tX == null)
{
return 0;
}
else
{
Tuple tY = y as Tuple;
return Comparer.Default.Compare(tX.Item2, tY.Item2);
}
}
}
public class Example
{
public static void Main()
{
Tuple>[] scores =
{ new Tuple>("Jack", 78),
new Tuple>("Abbey", 92),
new Tuple>("Dave", 88),
new Tuple>("Sam", 91),
new Tuple>("Ed", null),
new Tuple>("Penelope", 82),
new Tuple>("Linda", 99),
new Tuple>("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>());
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>("Jack", 78)
Tuple>("Abbey", 92)
Tuple>("Dave", 88)
Tuple>("Sam", 91)
Tuple>("Ed", Nullable())
Tuple>("Penelope", 82)
Tuple>("Linda", 99)
Tuple>("Judith", 84) |]
printfn "The values in unsorted order:"
for score in scores do
printfn $"{score}"
printfn ""
Array.Sort(scores, ScoreComparer>())
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)
Remarks
This member is an explicit interface member implementation. It can be used only when the Tuple
Although this method can be called directly, it is most commonly called by collection sorting methods that include IComparer parameters to order the members of a collection. For example, it is called by the Array.Sort(Array, IComparer) method and the Add method of a SortedList object that is instantiated by using the SortedList.SortedList(IComparer) constructor.
Caution
The IStructuralComparable.CompareTo method is intended for use in sorting operations. It should not be used when the primary purpose of a comparison is to determine whether two objects are equal. To determine whether two objects are equal, call the IStructuralEquatable.Equals method.