Search and sort data stored in the Generic type (Generic)

Source: Internet
Author: User

It is mainly to combine the two sets of numbers into the list <T> (generic) for search and sorting.

Example

Search for List <Person> people = new List <Person> ();

People. Add (new Person (50, "Fred "));
People. Add (new Person (30, "John "));
People. Add (new Person (26, "Andrew "));
People. Add (new Person (24, "Xavier "));
People. Add (new Person (5, "Mark "));
People. Add (new Person (6, "Cameron "));

List <Person> young = people. FindAll (delegate (Person p) {return p. age <25 | p. name. Contains ("F ");});


Note:The above code is to search for records at 25 age, or whose names contain the "F" letter. Be sure to have a return statement and do not report an error!

SortPeople. Sort (delegate (Person p1, Person p2) {return p1.age. CompareTo (p2.age );});

Note:The above code is sorted by age in ascending order, and the positions of p1.age and p2.age are changed to descending order.
For example, people. Sort (delegate (Person p1, Person p2) {return p2.age. CompareTo (p1.age );});

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.