Tuple<T1,T2,T3,T4,T5>.IStructuralComparable.CompareTo 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的比较器将当前的 Tuple<T1,T2,T3,T4,T5> 对象与指定对象进行比较,并返回一个整数,该整数指示当前对象在排序顺序中的位置是在指定对象之前、之后还是与其相同。
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,T4,T5> 对象。
示例
以下示例创建一组 Tuple<T1,T2,T3,T4,T5> 对象,其中包含用于在美国职业足球中跑回的职业统计数据。 5 元组的组件包括玩家的姓名、他玩的游戏数、携带次数或尝试次数、获得码总数以及得分的触地次数。 该示例按未排序的顺序显示数组中每个元组的组件,对数组进行排序,然后调用以 ToString 排序顺序显示每个元组。 为了对数组进行排序,该示例定义了一个泛型 YardsGained
类,该类实现 IComparer 接口,并按其第四个分量的值( (码的值)按其第四个组件的值(而不是第一个组件) )对对象进行降序排序 Tuple<T1,T2,T3,T4,T5> 。 请注意,该示例不直接调用 IStructuralComparable.CompareTo 该方法。 此方法由 Array.Sort(Array, IComparer) 数组中每个元素的方法隐式调用。
using System;
using System.Collections;
using System.Collections.Generic;
public class YardsGained<T1, T2, T3, T4, T5> : IComparer
{
public int Compare(object x, object y)
{
Tuple<T1, T2, T3, T4, T5> tX = x as Tuple<T1, T2, T3, T4, T5>;
if (tX == null)
{
return 0;
}
else
{
Tuple<T1, T2, T3, T4, T5> tY = y as Tuple<T1, T2, T3, T4, T5>;
return -1 * Comparer<T4>.Default.Compare(tX.Item4, tY.Item4);
}
}
}
public class Example
{
public static void Main()
{
// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
Tuple<string, int, int, int, int>[] runningBacks =
{ Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) };
// Display the array in unsorted order.
Console.WriteLine("The values in unsorted order:");
foreach (var runningBack in runningBacks)
Console.WriteLine(runningBack.ToString());
Console.WriteLine();
// Sort the array
Array.Sort(runningBacks, new YardsGained<string, int, int, int, int>());
// Display the array in sorted order.
Console.WriteLine("The values in sorted order:");
foreach (var runningBack in runningBacks)
Console.WriteLine(runningBack.ToString());
}
}
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
open System
open System.Collections
open System.Collections.Generic
type YardsGained<'T1, 'T2, 'T3, 'T4, 'T5>() =
interface IComparer with
member _.Compare(x, y) =
match x with
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5> as tX ->
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5>
-1 * Comparer<'T4>.Default.Compare(tX.Item4, tY.Item4)
| _ -> 0
// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
let runningBacks =
[| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]
// Display the array in unsorted order.
printfn "The values in unsorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
printfn ""
// Sort the array
Array.Sort(runningBacks, YardsGained<string, int, int, int, int>())
// Display the array in sorted order.
printfn "The values in sorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
Imports System.Collections
Imports System.Collections.Generic
Public Class YardsGained(Of T1, T2, T3, T4, T5) : Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Dim tX As Tuple(Of T1, T2, T3, T4, T5) = TryCast(x, Tuple(Of T1, T2, T3, T4, T5))
If tX Is Nothing Then
Return 0
Else
Dim tY As Tuple(Of T1, T2, T3, T4, T5) = DirectCast(y, Tuple(Of T1, T2, T3, T4, T5))
Return -1 * Comparer(Of T4).Default.Compare(tx.Item4, tY.Item4)
End If
End Function
End Class
Module Example
Public Sub Main()
' Organization of runningBacks 5-tuple:
' Component 1: Player name
' Component 2: Number of games played
' Component 3: Number of attempts (carries)
' Component 4: Number of yards gained
' Component 5: Number of touchdowns
Dim runningBacks() =
{ Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) }
' Display the array in unsorted order.
Console.WriteLine("The values in unsorted order:")
For Each runningBack In runningBacks
Console.WriteLine(runningBack.ToString())
Next
Console.WriteLine()
' Sort the array
Array.Sort(runningBacks, New YardsGained(Of String, Integer, Integer, Integer, Integer)())
' Display the array in sorted order.
Console.WriteLine("The values in sorted order:")
For Each runningBack In runningBacks
Console.WriteLine(runningBack.ToString())
Next
End Sub
End Module
' The example displays the following output:
' The values in unsorted order:
' (Payton, Walter, 190, 3838, 16726, 110)
' (Sanders, Barry, 153, 3062, 15269, 99)
' (Brown, Jim, 118, 2359, 12312, 106)
' (Dickerson, Eric, 144, 2996, 13259, 90)
' (Faulk, Marshall, 176, 2836, 12279, 100)
'
' The values in sorted order:
' (Payton, Walter, 190, 3838, 16726, 110)
' (Sanders, Barry, 153, 3062, 15269, 99)
' (Dickerson, Eric, 144, 2996, 13259, 90)
' (Brown, Jim, 118, 2359, 12312, 106)
' (Faulk, Marshall, 176, 2836, 12279, 100)
注解
此成员是显式接口成员的实现。 它只能在 Tuple<T1,T2,T3,T4,T5> 实例被强制转换为 IStructuralComparable 接口时使用。
虽然可以直接调用此方法,但通常由包含参数的集合排序方法 IComparer 调用此方法,以便对集合的成员进行排序。 例如,它由Array.Sort(Array, IComparer)该方法和Add通过使用SortedList.SortedList(IComparer)构造函数实例化的对象的方法SortedList调用。
注意
该方法 IStructuralComparable.CompareTo 用于排序操作。 当比较的主要用途是确定两个对象是否相等时,不应使用它。 若要确定两个对象是否相等,请调用 IStructuralEquatable.Equals(Object, IEqualityComparer) 该方法。