IComparable .CompareTo(T) 方法


IComparable<T>.CompareTo(T) 方法

定義

將當前實例與同一類型的另一個對象進行比較,並返回一個整數,該整數指示當前實例在排序順序中的位置是位於另一個對象之前、之后還是與其位置相同。

C#
public int CompareTo (T other);

參數

other
T

與此實例進行比較的對象。

返回

一個值,指示要比較的對象的相對順序。 返回值的含義如下:

含義
小於零 此實例在排序順序中位於 other 之前。
此實例在排序順序中的位置與 other 相同。
大於零 此實例在排序順序中位於 other 之后。

示例

下面的代碼示例演示了一個簡單IComparable<T> Temperature對象的實現。 該示例創建一個SortedList<TKey,TValue>具有Temperature對象鍵的字符串集合,並將多對的溫度和字符串按順序添加到列表中。 在對Add方法的調用中SortedList<TKey,TValue> ,集合使用IComparable<T>實現對列表條目進行排序,然后按溫度的增加順序顯示這些條目。

C#
using System; using System.Collections.Generic; public class Temperature : IComparable<Temperature> { // Implement the generic CompareTo method with the Temperature // class as the Type parameter. // public int CompareTo(Temperature other) { // If other is not a valid object reference, this instance is greater. if (other == null) return 1; // The temperature comparison depends on the comparison of // the underlying Double values. return m_value.CompareTo(other.m_value); } // Define the is greater than operator. public static bool operator > (Temperature operand1, Temperature operand2) { return operand1.CompareTo(operand2) == 1; } // Define the is less than operator. public static bool operator < (Temperature operand1, Temperature operand2) { return operand1.CompareTo(operand2) == -1; } // Define the is greater than or equal to operator. public static bool operator >= (Temperature operand1, Temperature operand2) { return operand1.CompareTo(operand2) >= 0; } // Define the is less than or equal to operator. public static bool operator <= (Temperature operand1, Temperature operand2) { return operand1.CompareTo(operand2) <= 0; } // The underlying temperature value. protected double m_value = 0.0; public double Celsius { get { return m_value - 273.15; } } public double Kelvin { get { return m_value; } set { if (value < 0.0) { throw new ArgumentException("Temperature cannot be less than absolute zero."); } else { m_value = value; } } } public Temperature(double kelvins) { this.Kelvin = kelvins; } } public class Example { public static void Main() { SortedList<Temperature, string> temps = new SortedList<Temperature, string>(); // Add entries to the sorted list, out of order. temps.Add(new Temperature(2017.15), "Boiling point of Lead"); temps.Add(new Temperature(0), "Absolute zero"); temps.Add(new Temperature(273.15), "Freezing point of water"); temps.Add(new Temperature(5100.15), "Boiling point of Carbon"); temps.Add(new Temperature(373.15), "Boiling point of water"); temps.Add(new Temperature(600.65), "Melting point of Lead"); foreach( KeyValuePair<Temperature, string> kvp in temps ) { Console.WriteLine("{0} is {1} degrees Celsius.", kvp.Value, kvp.Key.Celsius); } } } /* This example displays the following output: Absolute zero is -273.15 degrees Celsius. Freezing point of water is 0 degrees Celsius. Boiling point of water is 100 degrees Celsius. Melting point of Lead is 327.5 degrees Celsius. Boiling point of Lead is 1744 degrees Celsius. Boiling point of Carbon is 4827 degrees Celsius. */ 

注解

CompareTo提供強類型的比較方法以對泛型集合對象的成員進行排序。 因此,通常不會直接從開發人員代碼中調用它。 相反,它由List<T>.Sort()Add等方法自動調用。

此方法只是定義,必須由特定的類或值類型實現才能使其生效。 "返回值" 部分中指定的比較("先於"、"與" 的位置相同)的含義取決於特定實現。

按照定義,任何對象比較大於null,兩個 null 引用的比較結果相等。

實施者說明

對於對象 A、B 和 C,必須滿足以下條件: 需要CompareTo (A)以返回零。

如果CompareTo (B)返回零,則需要CompareTo (A)以返回零。

如果CompareTo (B)返回零, CompareTo (c)返回零,則需要CompareTo (c)以返回零。

如果CompareTo (B)返回的值不是零,則需要CompareTo (A)來返回相反的符號值。

如果CompareTo (B)返回一個不等於零x的值,而CompareTo (c)返回與相同的符號y x值,則需要CompareTo (c)來返回與相同的符號的值x和。y

調用方說明

CompareTo(T)使用方法來確定類的實例的排序。

適用於

.NET Core

3.1 3.0 2.2 2.1 2.0 1.1 1.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0

.NET Standard

2.1 2.0 1.6 1.5 1.4 1.3 1.2 1.1 1.0

UWP

10.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

另請參閱


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM