Tuple<T1>.IStructuralComparable.CompareTo(Object, IComparer) 方法

定義

使用指定的比較子將目前的 Tuple<T1> 物件和指定的物件進行比較,並且傳回一個整數,表示目前的物件在排序順序中位於指定之物件的前面、後面還是相的位置。

 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> 物件。

範例

下列範例會定義名為 的 DescendingComparer 泛型類別,以實作 IComparer<T> 介面。 DescendingComparer 藉由反轉特定類型之預設比較子傳回的值,以遞減而非遞增順序排序物件。 然後,泛型 DescendingComparer 類別的實例會傳遞至 方法, Array.Sort(Array, IComparer) 以遞減順序排序物件的陣列 Tuple<T1> 。 請注意,此範例不會直接呼叫 IStructuralComparable.CompareTo 方法。 這個方法是由 陣列中每個元素的 方法隱含 Array.Sort(Array, IComparer) 呼叫。

using System;
using System.Collections.Generic;

public class DescendingComparer<T> : IComparer<T>
{
    public int Compare(T x, T y) 
    {
        return -1 * Comparer<T>.Default.Compare(x, y);
    }
}

class CompareTo2
{
   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, new DescendingComparer<Tuple<Double>>());

       Console.WriteLine("The values sorted in descending 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 sorted in descending order:
//         Infinity
//         13.54
//         1.934E-17
//         4.94065645841247E-324
//         -3.588E-12
//         -189.42993
//         -Infinity
//         NaN
open System
open System.Collections.Generic

type DescendingComparer<'T>() =
    interface IComparer<'T> with
        member _.Compare(x: 'T, y: 'T) = 
            -1 * Comparer<'T>.Default.Compare(x, y)

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, DescendingComparer<Tuple<Double>>())

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 sorted in descending order:
//         Infinity
//         13.54
//         1.934E-17
//         4.94065645841247E-324
//         -3.588E-12
//         -189.42993
//         -Infinity
//         NaN
Imports System.Collections.Generic

Public Class DescendingComparer(Of T) : Implements IComparer(Of T)
    Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements IComparer(Of T).Compare
        Return -1 * Comparer(Of T).Default.Compare(x, y)
    End Function
End Class

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 As Tuple(Of Double) In values
            If value IsNot Nothing Then
                Console.WriteLine("   {0}", value.Item1)
            Else
                Console.WriteLine("   <null>")
            End If
        Next
        Console.WriteLine()

        Array.Sort(values, New DescendingComparer(Of Tuple(Of Double)))

        Console.WriteLine("The values sorted in descending order:")
        For Each value As Tuple(Of Double) 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 sorted in descending order:
'         Infinity
'         13.54
'         1.934E-17
'         4.94065645841247E-324
'         -3.588E-12
'         -189.42993
'         -Infinity
'         NaN

備註

雖然可以直接呼叫這個方法,但最常由集合排序方法呼叫,其中包含 IComparer 排序集合成員的參數。 例如,它是由 Array.Sort(Array, IComparer) 方法呼叫,以及 Add 使用 SortedList.SortedList(IComparer) 建構函式具現化之 物件的 方法 SortedList

警告

方法 IStructuralComparable.CompareTo 適用于排序作業。 當比較的主要用途是判斷兩個物件是否相等時,就不應該使用它。 若要判斷兩個物件是否相等,請呼叫 IStructuralEquatable.Equals 方法。

適用於

另請參閱