Tuple<T1>.IComparable.CompareTo(Object) 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前 Tuple<T1> 对象与指定对象进行比较,并返回一个整数,该整数指示当前对象在排序顺序中的位置是位于指定对象之前、之后还是与其位置相同。
 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 -> intFunction CompareTo (obj As Object) As Integer Implements IComparable.CompareTo参数
- obj
- Object
要与当前实例进行比较的对象。
返回
一个带符号整数,指示此实例和 obj 在排序顺序中的相对位置,如下表所示。
| 值 | 说明 | 
|---|---|
| 负整数 | 此实例位于 obj之前。 | 
| 零 | 此实例在排序顺序中的位置与 obj相同。 | 
| 正整数 | 此实例位于 obj之后。 | 
实现
例外
              obj 不是 Tuple<T1> 对象。
示例
以下示例创建一个单一实例数组,其组件为值 Double 。 它按未排序的顺序显示每个元组组件的值,对数组进行排序,然后按排序顺序显示值。 请注意,该示例不直接调用 Tuple<T1>.IComparable.CompareTo 方法。 此方法由 Sort(Array) 数组中每个元素的 方法隐式调用。
using System;
class Example
{
   static void Main()
   {
      Tuple<Double>[] values = { Tuple.Create(13.54),
                                 Tuple.Create(Double.NaN),
                                 Tuple.Create(-189.42993),
                                 Tuple.Create(Double.PositiveInfinity),
                                 Tuple.Create(Double.Epsilon),
                                 Tuple.Create(1.934E-17),
                                 Tuple.Create(Double.NegativeInfinity),
                                 Tuple.Create(-0.000000000003588),
                                 null };
      Console.WriteLine("The values in unsorted order:");
      foreach (var value in values)
         if (value != null)
            Console.WriteLine("   {0}", value.Item1);
         else
            Console.WriteLine("   <null>");
      Console.WriteLine();
      Array.Sort(values);
      Console.WriteLine("The values in sorted order:");
      foreach (var value in values)
         if (value != null)
            Console.WriteLine("   {0}", value.Item1);
         else
            Console.WriteLine("   <null>");
   }
}
// The example displays the following output:
//      The values in unsorted order:
//         13.54
//         NaN
//         -189.42993
//         Infinity
//         4.94065645841247E-324
//         1.934E-17
//         -Infinity
//         -3.588E-12
//
//      The values in sorted order:
//         NaN
//         -Infinity
//         -189.42993
//         -3.588E-12
//         4.94065645841247E-324
//         1.934E-17
//         13.54
//         Infinity
open System
let values = 
    [| Tuple.Create 13.54
       Tuple.Create Double.NaN
       Tuple.Create -189.42993
       Tuple.Create Double.PositiveInfinity
       Tuple.Create Double.Epsilon
       Tuple.Create 1.934E-17
       Tuple.Create Double.NegativeInfinity
       Tuple.Create -0.000000000003588
       null |]
printfn "The values in unsorted order:"
for value in values do
    printfn $"   %A{value.Item1}"
printfn ""
Array.Sort values
printfn "The values sorted in descending order:"
for value in values do
    printfn $"   %A{value.Item1}"
// The example displays the following output:
//      The values in unsorted order:
//         13.54
//         NaN
//         -189.42993
//         Infinity
//         4.94065645841247E-324
//         1.934E-17
//         -Infinity
//         -3.588E-12
//
//      The values in sorted order:
//         NaN
//         -Infinity
//         -189.42993
//         -3.588E-12
//         4.94065645841247E-324
//         1.934E-17
//         13.54
//         Infinity
Module Example
    Sub Main()
        Dim values() = { Tuple.Create(13.54),
                         Tuple.Create(Double.NaN),
                         Tuple.Create(-189.42993),
                         Tuple.Create(Double.PositiveInfinity),
                         Tuple.Create(Double.Epsilon),
                         Tuple.Create(1.934E-17),
                         Tuple.Create(Double.NegativeInfinity),
                         Tuple.Create(-0.000000000003588),
                         Nothing}
        Console.WriteLine("The values in unsorted order:")
        For Each value In values
            If value IsNot Nothing Then
                Console.WriteLine("   {0}", value.Item1)
            Else
                Console.WriteLine("   <null>")
            End If
        Next
        Console.WriteLine()
        Array.Sort(values)
        Console.WriteLine("The values in sorted order:")
        For Each value In values
            If value IsNot Nothing Then
                Console.WriteLine("   {0}", value.Item1)
            Else
                Console.WriteLine("   <null>")
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'      The values in unsorted order:
'         13.54
'         NaN
'         -189.42993
'         Infinity
'         4.94065645841247E-324
'         1.934E-17
'         -Infinity
'         -3.588E-12
'
'      The values in sorted order:
'         NaN
'         -Infinity
'         -189.42993
'         -3.588E-12
'         4.94065645841247E-324
'         1.934E-17
'         13.54
'         Infinity
注解
此成员是显式接口成员的实现。 它只能在 Tuple<T1> 实例被强制转换为 IComparable 接口时使用。
此方法提供 IComparable.CompareTo 类的 Tuple<T1> 实现。 虽然 可以直接调用 方法,但它通常由集合排序方法(如 Array.Sort(Array) 和 SortedList.Add)的默认重载调用,以便对集合的成员进行排序。
注意
方法 Tuple<T1>.IComparable.CompareTo 适用于排序操作。 当比较的主要目的是确定两个对象是否相等时,不应使用它。 若要确定两个对象是否相等,请调用 Equals 方法。
方法 Tuple<T1>.IComparable.CompareTo 使用默认的对象比较器。