Tuple<T1,T2,T3,T4,T5,T6>.IComparable.CompareTo(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將當前 Tuple<T1,T2,T3,T4,T5,T6> 物件與指定物件比較,並回傳一個整數,表示該物件在排序順序中是否位於指定物件之前、之後或相同位置。
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,T6> 物件。
範例
以下範例建立一個包含 1960 至 2000 年內三個美國城市人口資料的 Tuple<T1,T2,T3,T4,T5,T6> 物件陣列。 這六個組成部分包括城市名稱後接1960年至2000年間每十年的人口。 範例中每個元組的組成部分以未排序順序顯示,然後對陣列進行排序,然後呼叫 ToString 該方法以排序順序顯示每個元組。 輸出顯示陣列已依名稱排序,名稱是第一個元件。 請注意,範例中並未直接呼叫該 IComparable.CompareTo(Object) 方法。 此方法由 方法 Sort(Array) 隱含呼叫,針對陣列中的每個元素。
using System;
public class Example
{
public static void Main()
{
// Create array of sextuple with population data for three U.S.
// cities, 1960-2000.
Tuple<string, int, int, int, int, int>[] cities =
{ Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820),
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278),
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) };
// Display array in unsorted order.
Console.WriteLine("In unsorted order:");
foreach (var city in cities)
Console.WriteLine(city.ToString());
Console.WriteLine();
Array.Sort(cities);
// Display array in sorted order.
Console.WriteLine("In sorted order:");
foreach (var city in cities)
Console.WriteLine(city.ToString());
}
}
// The example displays the following output:
// In unsorted order:
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
//
// In sorted order:
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
open System
// Create array of sextuple with population data for three U.S.
// cities, 1960-2000.
let cities =
[| Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) |]
// Display array in unsorted order.
printfn "In unsorted order:"
for city in cities do
printfn $"{city}"
printfn ""
Array.Sort cities
// Display array in sorted order.
printfn "In sorted order:"
for city in cities do
printfn $"{city}"
// The example displays the following output:
// In unsorted order:
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
//
// In sorted order:
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
Module Example
Public Sub Main()
' Create array of sextuple with population data for three U.S.
' cities, 1960-2000.
Dim cities() =
{ Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820),
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278),
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) }
' Display array in unsorted order.
Console.WriteLine("In unsorted order:")
For Each city In cities
Console.WriteLine(city.ToString())
Next
Console.WriteLine()
Array.Sort(cities)
' Display array in sorted order.
Console.WriteLine("In sorted order:")
For Each city In cities
Console.WriteLine(city.ToString())
Next
End Sub
End Module
' The example displays the following output:
' In unsorted order:
' (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
' (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
' (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
'
' In sorted order:
' (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
' (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
' (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
備註
此成員為明確介面成員實作。 它只能在實 Tuple<T1,T2,T3,T4,T5,T6> 例投射到 IComparable 介面時使用。
此方法提供 IComparable.CompareTo 類別的實作 Tuple<T1,T2,T3,T4,T5,T6> 。 雖然此方法可直接呼叫,但通常由預設的集合排序方法(如 Array.Sort(Array) 和 SortedList.Add)呼叫,以排序集合成員。
注意事項
此 IComparable.CompareTo 方法主要用於排序操作。 當比較的主要目的是判斷兩個物體是否相等時,不應使用此方法。 要判斷兩個物體是否相等,請呼叫該 Tuple<T1,T2,T3,T4,T5,T6>.Equals(Object) 方法。
此 IComparable.CompareTo(Object) 方法使用預設的物件比較器來比較每個元件。