This article summarizes:
1: The concept of comparison and sequencing;
2:icomparable and IComparer;
Generic implementations of 3:icomparable and IComparer icomparable<t> and icomparer<t>;
1: The concept of comparison and sorting
Comparison: Two entity classes are compared by >,=,<.
Sort: In the collection class, sort the entities in the collection class. The algorithm based on sorting is based on the comparison function provided by the entity class.
The basic type provides a default comparison algorithm, such as String, which provides a comparison by letter, and Int provides a comparison by an integer size.
2:icomparable and IComparer
When we create our own entity classes, such as student, which by default want to sort by age, you need to implement the IComparable interface for the entity class.
    
    Class Student:icomparable {PublicString Name {get;        set;} public int Age { get; set;} #region IComparable members public int CompareTo (object obj) {Student 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 that the CompareTo method in the above code has a comment code, in fact, this function can be used entirely instead of the comment code, because the use of the default method of comparison of shaping. This comment code is not used here to better illustrate how the comparator works.
    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 button1_click ( object sender, EventArgs e) {Studentlist.sort (); foreach (Student item in studentlist) {this.textbox1.text + = Item. Name + "----" +item. Age.tostring () + "\r\n";}}         
Operation Result:
A1----1
F1----2
B1----4
G1----5
OK, here's the question. What to do if you don't want to use your age as a comparator. This is the time for IComparer to work, and you can use IComparer to implement a custom comparator. As follows:
    Class Sortname:icomparer    {        #region IComparer members        int Compare (object y)        {             Return S1.Name.CompareTo (S2. Name); } #endregion}  
This time, we use the Sort method in order to provide this comparer:
Studentlist.sort (New Sortname ());
The result of the operation is:
A1----1
B1----4
F1----2
G1----5
Generic implementations of 3:icomparable and IComparer icomparable<t> and icomparer<t>
If we have a little experience, we will find the above code we use a collection class ArrayList that is not recommended. When generics come out, all non-generic collection classes have been advised not to use them as much as possible. As for the reason, we can see the clue from the code above.
Note See this compare function, such as:
        int Compare (object y)        {as            Student;            return S1.Name.CompareTo (S2. Name); }
We found this function to be boxed and unpacking. And this is going to affect performance. If there are thousands of complex entity objects in our collection, the performance that is consumed when sorting is objective. The emergence of generics, you can avoid unpacking and boxing.
Therefore, the above code in the ArrayList, should be replaced with List<t>,, corresponding, we should implement icomparable<t> and icomparer<t>. The final code should look like this:
public partialClass 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;Privatevoid Button1_Click (Object sender, EventArgs e) {Studentlist.sort (New Sortname ());foreach (Student itemIn Studentlist) {This.textBox1.Text + = Item. Name + "----"+item. Age.tostring () + "\ r \ n "; } } }Class Student:icomparable<student> {PublicString Name {get; set;} public int Age {get; Span style= "color: #0000ff;" >set; } #region icomparable<student> members public int CompareTo (Student other) {return Age.compareto (Other). Age); } #endregion} class sortname:icomparer<student> {#region icomparer<student > Members public int Compare (Student x, Student y) { Span style= "color: #0000ff;" >return X.name.compareto (y.name); } #endregion} 
 To: This article is based on the creative Commons Attribution 2.5 China Mainland license published, welcome reprint, deduction or for commercial purposes, but must retain the attribution of this article http:// Www.cnblogs.com/luminji (contains links). If you have any questions or authorization to negotiate, please leave me a message. 
 Compare and sort (IComparable and IComparer and their generic implementations)