Tuple<T1,T2>.IComparable.CompareTo(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前 Tuple<T1,T2> 对象与指定对象进行比较,并返回一个整数,该整数指示当前对象是之前、之后还是与排序顺序中的指定对象位于同一位置。
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> 。
示例
以下示例创建一个由学生的姓名和测试分数组成的对象数组 Tuple<T1,T2> 。 它以未排序的顺序显示数组中每个元组的组件,对数组进行排序,然后调用以 ToString 排序顺序显示每个元组的值。 输出显示数组已按其第一个组件进行排序。 请注意,该示例不直接调用 IComparable.CompareTo(Object) 该方法。 此方法由 Sort(Array) 数组中每个元素的方法隐式调用。
using System;
public class Example
{
public static void Main()
{
Tuple<string, Nullable<int>>[] scores =
{ new Tuple<string, Nullable<int>>("Jack", 78),
new Tuple<string, Nullable<int>>("Abbey", 92),
new Tuple<string, Nullable<int>>("Dave", 88),
new Tuple<string, Nullable<int>>("Sam", 91),
new Tuple<string, Nullable<int>>("Ed", null),
new Tuple<string, Nullable<int>>("Penelope", 82),
new Tuple<string, Nullable<int>>("Linda", 99),
new Tuple<string, Nullable<int>>("Judith", 84) };
Console.WriteLine("The values in unsorted order:");
foreach (Tuple<string, Nullable<int>> score in scores)
Console.WriteLine(score.ToString());
Console.WriteLine();
Array.Sort(scores);
Console.WriteLine("The values in sorted order:");
foreach (Tuple<string, Nullable<int>> 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:
// (Abbey, 92)
// (Dave, 88)
// (Ed, )
// (Jack, 78)
// (Judith, 84)
// (Linda, 99)
// (Penelope, 82)
// (Sam, 91)
open System
let scores =
[| Tuple<string, Nullable<int>>("Jack", 78)
Tuple<string, Nullable<int>>("Abbey", 92)
Tuple<string, Nullable<int>>("Dave", 88)
Tuple<string, Nullable<int>>("Sam", 91)
Tuple<string, Nullable<int>>("Ed", Nullable())
Tuple<string, Nullable<int>>("Penelope", 82)
Tuple<string, Nullable<int>>("Linda", 99)
Tuple<string, Nullable<int>>("Judith", 84) |]
printfn "The values in unsorted order:"
for score in scores do
printfn $"{score}"
printfn ""
Array.Sort scores
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:
// (Abbey, 92)
// (Dave, 88)
// (Ed, )
// (Jack, 78)
// (Judith, 84)
// (Linda, 99)
// (Penelope, 82)
// (Sam, 91)
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)
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:
' (Abbey, 92)
' (Dave, 88)
' (Ed, )
' (Jack, 78)
' (Judith, 84)
' (Linda, 99)
' (Penelope, 82)
' (Sam, 91)
注解
此成员是显式接口成员实现。 仅当实例强制转换为Tuple<T1,T2>接口时IComparable,才能使用它。
此方法提供 IComparable.CompareTo 类的 Tuple<T1,T2> 实现。 尽管可以直接调用该方法,但通常由集合排序方法的默认重载(例如 Array.Sort(Array) 以及 SortedList.Add)对集合的成员进行排序。
Caution
该方法 Tuple<T1,T2>.IComparable.CompareTo 用于排序操作。 当比较的主要目的是确定两个对象是否相等时,不应使用它。 若要确定两个对象是否相等,请调用该方法 Equals 。
该方法 Tuple<T1,T2>.IComparable.CompareTo 使用默认对象比较器来比较每个组件。