Object comparison and sorting (2): Implement icomparable and icomparer interfaces and comparer class

Source: Internet
Author: User
By default, the equals (Object O) method of the object (provided by the Base Class Object) is used to compare whether two object variables reference the same object. We must define our own object comparison methods. The icomparable and icompare interfaces are. net Framework. The differences between the two interfaces are as follows: 1. icomparable is implemented in the class of the object to be compared. You can compare this object with another object. 2. icomparer is implemented in a separate class and can compare any two objects. Generally, we use icomparable to give a default comparison of classes. Code , Use other classes to provide non-default comparison code. 1. icomparable provides the int compareto (Object OBJ) method ). This method accepts an object, so this interface can be implemented, for example, to send the person object to it, indicating whether the person is older or younger than the current person. In fact, this method returns an int, so the following code shows that the second person is older or younger. If (person1.compareto (person2) = 0) {console. writeline ("Same Age");} else if (person1.compareto (person2)> 0) {console. writeline ("person 1 is older");} else {console. writeline ("person1 is younger");} 2. icomparer also provides a compare () method (). this method accepts two objects and returns an integer, which is the same as compareto. For objects that support icomparer, you can use the following code: If (personcomparer. compare (person1, person2) = 0) {console. writeline ("Same Age");} else if (personcomparer. compare (person1, person2)> 0) {console. writeline ("person 1 is older");} else {console. writeline ("person1 is younger");} in both cases, the parameter provided to the method is system. object type. That is to say, two objects of any type can be compared. Therefore, some types of comparison are usually required before the returned results. If an error type is used, an exception is thrown. In fact, we use the generic interface icomparable <t> to omit object conversion. You can refer to the following diary. Iii ,. net Framework provides the default implementation method of the icomparer interface on the class comparer. The class comparer is located in the system. in the collections namespace, you can compare simple types and any types that support the icomparable interface with specific cultures. For example, you can use the following code: String firststring = "first string"; string secondstring = "second string"; comparer. default. compare (firststring, secondstring); int firstnumber = 35; int secondnumber = 23; comparer. default. compare (firstnumber, secondnumber); comparer is used here. the default static member obtains an instance of the comparer class and then compares it using the compare () method. When using comparer, you must use comparable types. For example, if you try to compare firststring and firstnumber, an exception is generated. The following are some considerations for this class: 1. Check the objects passed to comparer. Compare () to see if they support icomparable. If so, use this implementation code. 2. A null value is allowed, indicating that it is smaller than other objects. 3. the string is processed according to the current culture. To process strings according to different cultures (or languages), The comparer class must use its constructor for instance to transmit the system. Globalization. cultureinfo object of the specified culture. 4. strings must be case-sensitive during processing. To process strings in case-insensitive mode, use the caseinsensitivecomparer class, which works in the same way.

from: http://www.cnblogs.com/eagle1986/archive/2012/02/03/2278455.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.