Tuple<T1,T2,T3>.IStructuralComparable.CompareTo 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的比较器将当前的 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 之后。
|
实现
例外
other
不是 Tuple<T1,T2,T3> 对象。
示例
以下示例创建一个由学生的姓名、平均测试分数和测试数组成的对象数组 Tuple<T1,T2,T3> 。 它按未排序顺序显示数组中每个元组的组件,对数组进行排序,然后调用以 ToString 排序顺序显示每个元组的值。 为了对数组进行排序,该示例定义一个泛型 ScoreComparer
类,该类实现 IComparer 接口,并按其第二个组件的值而不是第一个组件的值按升序对对象进行排序 Tuple<T1,T2,T3> 。 请注意,该示例不直接调用 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(IComparer)构造函数实例化的对象的方法SortedList调用。
注意
该方法 Tuple<T1,T2,T3>.IStructuralComparable.CompareTo 用于排序操作。 当比较的主要用途是确定两个对象是否相等时,不应使用它。 若要确定两个对象是否相等,请调用 Tuple<T1,T2,T3>.IStructuralEquatable.Equals 该方法。