Tuple<T1,T2,T3,T4,T5>.IComparable.CompareTo(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
比较当前 Tuple<T1,T2,T3,T4,T5> 对象与指定对象,并返回一个整数,该整数指示当前对象在排序顺序中的位置:是在指定对象之前、之后还是在与指定对象相同的位置。
virtual int System.IComparable.CompareTo(System::Object ^ obj) = IComparable::CompareTo;
int IComparable.CompareTo (object obj);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo
参数
- obj
- Object
要与当前实例进行比较的对象。
返回
一个带符号整数,指示此实例和 obj
在排序顺序中的相对位置,如下表所示。
值 | 说明 |
---|---|
负整数 | 此实例位于 obj 之前。
|
零 | 此实例在排序顺序中的位置与 obj 相同。
|
正整数 | 此实例位于 obj 之后。
|
实现
例外
obj
不是 Tuple<T1,T2,T3,T4,T5> 对象。
示例
以下示例创建一个 对象数组 Tuple<T1,T2,T3,T4,T5> ,其中包含在美国职业足球中后退的职业统计信息。 这五个组成部分包括玩家的姓名、他玩的游戏次数、携带或尝试次数、获得的总码数和得分的触地次数。 该示例按未排序顺序显示数组中每个元组的组件,对数组进行排序,然后调用 ToString 以按排序顺序显示每个元组。 输出显示数组已按名称(这是第一个组件)排序。 请注意,该示例不直接调用 IComparable.CompareTo 方法。 此方法由 Sort(Array) 数组中的每个元素的 方法隐式调用。
using System;
using System.Collections.Generic;
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);
// 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
// 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
// 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.Generic
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)
' 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:
' (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)
注解
此成员是显式接口成员的实现。 它只能在 Tuple<T1,T2,T3,T4,T5> 实例被强制转换为 IComparable 接口时使用。
此方法为 类提供 IComparable.CompareTo 实现 Tuple<T1,T2,T3,T4,T5> 。 虽然 可以直接调用 方法,但它通常由集合排序方法(如 和 SortedList.Add)Array.Sort(Array)的默认重载调用,以便对集合的成员进行排序。
注意
方法 IComparable.CompareTo 适用于排序操作。 当比较的主要目的是确定两个对象是否相等时,不应使用它。 若要确定两个 对象是否相等,请调用 Tuple<T1,T2,T3,T4,T5>.Equals(Object) 方法。
方法 IComparable.CompareTo(Object) 使用默认的对象比较器来比较每个组件。