Use of the Sort () method of the C # ArrayList

Source: Internet
Author: User
Tags implement sort
Keywords: ArrayList, IComparer, interface, C #

I saw someone on the internet to ask the use of IComparer interface, so wrote a small example, and you share. If there is any shortage, I hope you won't hesitate to correct me

1, build a structure of employees structure
private struct eployee{
public string name;
public int age;
public string sex;
}
2, new 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 Shi";
ep3.age=18;
ep3.sex= "Male";
3, add 3 "Employees" to the "Employee List";

ArrayList employeelist=new ArrayList ();
Employeelist.add (EP1);
Employeelist.add (EP2);
Employeelist.add (EP3);

All right, all set. Now I hope that the EmployeeList "employees" are sorted by age.
What do we do?
It's really simple, we don't have to implement a sort of method, bubble or something. ArrayList provides our out-of-the-box sorting method sort ();
It has three overloads, regardless of which one you use, at least you provide a comparer:icomparer to tell the sort method how you sort the employees.
This comparer must implement the interface: System.Collections.IComparer, the interface has only one member function that you need to implement.
The description is as follows:
[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)
You know, let's make it happen.
Private class MyEmployeeCompare:System.Collections.IComparer {
public int Compare (object X,object y) {
Return ((Eployee) x). age-((eployee) y). Age;
}

}
Because we compare the age of "employee", so we can safely write the implementation statement
Return ((Eployee) x). age-((eployee) y). Age;

Myemployeecompare employeecompare=new Myemployeecompare ();

OK now we can sort the employees by their age.

Employeelist.sort (Employeecompare);

#结束



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.