Object comparison and sorting (3): Implementing icomparable and icomparer generic Interfaces

Source: Internet
Author: User
1. Comparison of the concepts of comparison and sorting: compare two object classes by>, =, <. Sort: In the collection class, sort the objects in the Collection class. Sort based Algorithm Comparison functions provided by object classes. The basic types all provide the default comparison algorithm. For example, string provides the comparison by letter, and INT provides the comparison by integer size. 2: The icomparable and icomparer interfaces have been described in the previous log. Now we use an instance to explain it again. When we create our own entity class, such as student, by default, to sort objects by age, you need to implement the icomparable interface for the object class. Class student: icomparable {public string name {Get; set;} public int age {Get; set;} # region icomparable members public int compareto (Object OBJ) {student = OBJ as student; If (age> student. age) {return 1;} else if (age = student. age) {return 0;} else {return-1;} // return age. compareto (student. age) ;}# endregion} PS: note the preceding Code The compareto method in has an annotation code. In fact, this function can be replaced by this annotation code, because the default comparison method of integer is used. This annotation code is not used here to better illustrate the working principle of the comparator. Next, write a test case: Public form1 () {initializecomponent (); studentlist = new arraylist (); studentlist. add (new student () {age = 1, name = "A1"}); studentlist. add (new student () {age = 5, name = "g1"}); studentlist. add (new student () {age = 4, name = "B1"}); studentlist. add (new student () {age = 2, name = "f1"});} arraylist studentlist; private void button#click (Object sender, eventargs e) {student List. sort (); foreach (student item in studentlist) {This. textbox1.text + = item. name + "----" + item. age. tostring () + "\ r \ n" ;}} running result: A1 ---- 1 F1 ---- 2 B1 ---- 4 G1 ---- 5 OK. What should I do if I don't want to use age as the comparator. At this time, icomparer is used to implement a custom comparator. Class sortname: icomparer {public static icomparer default = new personcomparername (); # region icomparer members public int compare (Object X, object y) {student S1 = x as student; student S2 = y as student; return s1.name. compareto (s2.name) ;}# endregion} at this time, we provide this comparator for the sort method in order: studentlist. sort (sortname. default); the running result is: A1 ---- 1 B1 ---- 4 F1 ---- 2 G1 ---- 5 as mentioned in the previous diary. Generally, we use icomparable Provides the default comparison code for the class, and uses other classes to provide non-default comparison code. In the preceding example, the default comparison code (icomparable) is provided in student, so we can sort it by. Sort. At the same time, we can also provide non-default comparison code, such as sortname In the example (the icomparer interface is required), so we can sort by. Sort (sortname. Default. As long as the implementation is an icomparer interface, it can be used as a comparator parameter and thrown to sort (). 3: icomparable and icomparer generic implementation icomparable <t> and icomparer <t> if we have some experience, we will find that the above Code uses a collection class arraylist that is not recommended. When generics are available, we recommend that you do not use all non-generic collection classes as much as possible. As for the reason, we can also see some clues from the above Code. Check the Compare function, for example, public int compare (Object X, object y) {student S1 = x as student; Student S2 = y as student; return s1.name. compareto (s2.name);} we found that this function is packed and unpacked. This will affect the performance. If our set contains thousands of complex entity objects, the performance consumed during sorting is objective. The appearance of generics can avoid unpacking and packing. Therefore, the arraylist in the above Code should be replaced by list <t>. For the corresponding, we should implement icomparable <t> and icomparer <t>. The final code should be like: public partial class form1: FORM {public form1 () {initializecomponent (); studentlist = new list <student> (); studentlist. add (new student () {age = 1, name = "A1"}); studentlist. add (new student () {age = 5, name = "g1"}); studentlist. add (new student () {age = 4, name = "B1"}); studentlist. add (new student () {age = 2, name = "f1"}) ;}list <student> studentlist; private void button1_cl Ick (Object sender, eventargs e) {studentlist. sort (New sortname (); foreach (student item in studentlist) {This. textbox1.text + = item. name + "----" + item. age. tostring () + "\ r \ n" ;}}} class student: icomparable <student> {public string name {Get; Set ;}public int age {Get; set ;} # region icomparable <student> Members public int compareto (student other) {return age. compareto (Other. age) ;}# end Region} class sortname: icomparer <student >{# region icomparer <student> Members public int compare (student X, student y) {return X. name. compareto (Y. name) ;}# endregion} through the above example, we can know that implementing generic interfaces can make the code more concise.

from: http://www.cnblogs.com/eagle1986/archive/2011/12/06/2278531.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.