C # Use of the Sort () method of ArrayList

Source: Internet
Author: User

Reproduced in: http://www.kehui.net/html/article/26/26000.html

I saw someone asking me how to use the IComparer interface on the Internet, so I wrote a small example to share it with you. If you have any shortcomings, I hope you can correct them.

1. Create a structure employee structure
Private struct Eployee {
Public string name;
Public int age;
Public string sex;
}
2. Create 3 "employees"
Eployee ep1 = new Eployee ();
Ep1.name = "Xiao Zhang ";
Ep1.age = 21;
Ep1.sex = "male ";
Eployee ep2 = new Eployee ();
Ep2.name = "Lao Li ";
Ep2.age = 43;
Ep2.sex = "male ";
Eployee ep3 = new Eployee ();
Ep3.name = "Shi ";
Ep3.age = 18;
Ep3.sex = "male ";
3. Add three "employees" to the "employees" list ";
 
ArrayList EmployeeList = new ArrayList ();
EmployeeList. Add (ep1 );
EmployeeList. Add (ep2 );
EmployeeList. Add (ep3 );

Now everything is ready. Now we want "employees" in the EmployeeList to be sorted by age.
What should we do?
In fact, it is very simple. We do not need to implement a sorting method, bubble or something. ArrayList provides our ready-made sorting method Sort ();
It has three reloads. No matter which one is used, you need to provide at least one Comparer: IComparer; To tell the Sort method your sorting basis for "employees.
This Comparer must implement the interface: System. Collections. IComparer, which has only one member function to implement.
Description:
[Visual Basic]
Function Compare (_
ByVal x As Object ,_
ByVal y As Object _
) As Integer
[C #]
Int Compare (
Object x,
Object y
);
[C ++]
Int Compare (
Object * x,
Object * y
);
[JScript]
Function Compare (
X: Object,
Y: Object
): Int;
(You can refer to msdn)
If you know this, let's implement one.
Private class myEmployeeCompare: System. Collections. IComparer {
Public int Compare (object x, object y ){
Return (Eployee) x). age-(Eployee) y). age;
}

}
Because we compare the "employee" age, we can safely write implementation statements.
Return (Eployee) x). age-(Eployee) y). age;

MyEmployeeCompare EmployeeCompare = new myEmployeeCompare ();
 
OK. Now we can sort employees by age.
 
EmployeeList. Sort (EmployeeCompare );

# End

I am writing for the second time. I hope you will have a lot of support!

The above code is just a rough demonstration.

We hope to discuss more about dotNet technologies.
QQ: 14754875
Forum: http://www.shixm.com/bbs

Related Article

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.