# Tuple<T1,T2,T3>.IStructuralComparable.CompareTo(Object, IComparer) 方法

## 定义

`` 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);``
``Function CompareTo (other As Object, comparer As IComparer) As Integer Implements IStructuralComparable.CompareTo``

other
Object

comparer
IComparer

#### 返回

Value 说明Description

Zero 此实例在排序顺序中的位置与 `other` 相同。This instance and `other` have the same position in the sort order.

#### 例外

`other` 不是 Tuple<T1,T2,T3> 对象。`other` is not a Tuple<T1,T2,T3> object.

## 示例

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

## 注解

The Tuple<T1,T2,T3>.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. 若要确定两个对象是否相等，请调用 Tuple<T1,T2,T3>.IStructuralEquatable.Equals 方法。To determine whether two objects are equal, call the Tuple<T1,T2,T3>.IStructuralEquatable.Equals method.