In C#,CompareTo
Methods are usually used to compare the order of the current object with another object. This method is widely used to implementIComparable<T>
orIComparer<T>
In the interface class so that objects can be sorted or compared.
1. Implement IComparable<T>
If there is a class that needs to be implementedIComparable<T>
Interface, need to provide aCompareTo
Methods to define natural sorting rules between class instances.CompareTo
The method returns a negative number to indicate that the current object is smaller than the parameter object, zero means that the two are equal, and positive number to indicate that the current object is larger than the parameter object.
For example, for a class containing a namePerson
, can be implemented like this:
public class Person : IComparable<Person> { public string Name { get; set; } public int CompareTo(Person other) { if (other == null) return 1; // Compare the names of two Person instances return (); } }
CompareTo
Method is used to compare the current instance with another instance. For strings,CompareTo
Returns an integer whose value indicates the relationship between the current object and the specified object.
- If the return value is 0, it means that the current string is equal to the compared string.
- If the return value is less than 0, it means that the current string is in alphabetical order before the comparison string.
- If the return value is greater than 0, it means that the current string is following the comparison string in alphabetical order.
Therefore, expressionm_szAPRCode.CompareTo("03002") != 0
The checked ism_szAPRCode
whetherNot equal to "03002"
. ifm_szAPRCode
and"03002"
Equal, thenCompareTo
The result will be 0 and the entire expression will become0 != 0
, this will returnfalse
; On the contrary, if the two are not equal,CompareTo
The result will not be 0, and the entire expression will returntrue
。
If you want to check whether it is equal, you can use it directly==
Operators to compare strings, for example:
if (m_Code == "03003") { // Execute when m_Code equals "03003"}
In addition, useCompareTo
When you need to pay attention to string sorting rules (such as culturally relevant sorting), as well as case sensitivity. Normally, use directly==
Comparing strings is an easier and less error-prone way.
2. Use CompareTo for comparison
Once thereCompareTo
Method, you can use it to perform comparison operations.
For example, in a LINQ query or array sort:
List<Person> people = new List<Person> { new Person { Name = "Alice" }, new Person { Name = "Bob" }, new Person { Name = "Charlie" } }; // Sort Person lists using CompareTo(); // Or use LINQvar sortedPeople = (p => p);
3. CompareTo in the basic type
Many built-in basic types such asint
, string
, DateTime
All have been realizedIComparable
Interface, and providesCompareTo
method.
For example:
string str1 = "apple"; string str2 = "banana"; int result = (str2); // Return negative number,because "apple" < "banana"
Anyway,CompareTo
is a very useful method to determine the relative order between objects. It is not only important in custom classes, but also essential in handling built-in types and sorting algorithms.
This is the end of this article about the usage of CompareTo in C#. For more related C# CompareTo content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!