Tuple<T1,T2,T3>.IComparable.CompareTo(Object) 方法

定义

比较当前 Tuple<T1,T2,T3> 对象与指定对象,并返回一个整数,该整数指示当前对象在排序顺序中的位置:是在指定对象之前、之后还是在与指定对象相同的位置。

 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

要与当前实例进行比较的对象。

返回

Int32

一个带符号整数,指示此实例和 obj 在排序顺序中的相对位置,如下表所示。

说明
负整数 此实例位于 obj 之前。
此实例在排序顺序中的位置与 obj 相同。
正整数 此实例位于 obj 之后。

实现

例外

obj 不是 Tuple<T1,T2,T3> 对象。

示例

以下示例创建一个对象数组 Tuple<T1,T2,T3> ,其组件由学生的姓名、平均测试分数和测试数组成。 它按未排序顺序显示数组中每个元组的组件,对数组进行排序,然后调用以 ToString 排序顺序显示每个元组。 输出显示数组已按其第一个组件进行排序。 请注意,该示例不直接调用 Tuple<T1,T2,T3>.IComparable.CompareTo 该方法。 此方法由 Sort(Array) 数组中每个元素的方法隐式调用。

using System;

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

      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:
//    (Abbey, 92.1, 9)
//    (Dave, 88.3, 9)
//    (Ed, 71.2, 5)
//    (Jack, 78.8, 8)
//    (Judith, 84.3, 9)
//    (Linda, 99, 9)
//    (Penelope, 82.9, 8)
//    (Sam, 91.7, 8)
open System

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

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:
//    (Abbey, 92.1, 9)
//    (Dave, 88.3, 9)
//    (Ed, 71.2, 5)
//    (Jack, 78.8, 8)
//    (Judith, 84.3, 9)
//    (Linda, 99, 9)
//    (Penelope, 82.9, 8)
//    (Sam, 91.7, 8)
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)

      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:
'    (Abbey, 92.1, 9)
'    (Dave, 88.3, 9)
'    (Ed, 71.2, 5)
'    (Jack, 78.8, 8)
'    (Judith, 84.3, 9)
'    (Linda, 99, 9)
'    (Penelope, 82.9, 8)
'    (Sam, 91.7, 8)

注解

此成员是显式接口成员的实现。 它只能在 Tuple<T1,T2,T3> 实例被强制转换为 IComparable 接口时使用。

此方法提供 IComparable.CompareToTuple<T1,T2,T3> 的实现。 尽管可以直接调用该方法,但通常由集合排序方法的默认重载(例如 Array.Sort(Array) 以及 SortedList.Add)对集合的成员进行排序。

注意

该方法 Tuple<T1,T2,T3>.IComparable.CompareTo 用于排序操作。 当比较的主要用途是确定两个对象是否相等时,不应使用它。 若要确定两个对象是否相等,请调用 Equals 该方法。

该方法 Tuple<T1,T2,T3>.IComparable.CompareTo 使用默认对象比较器来比较每个组件。

适用于

另请参阅