Library code snippets
Sorting and Searching Using C# Lists
C# Lists
public class Person
{
public int age;
public string name;
public Person(int age, string name)
{
this.age = age;
this.name = name;
}
}
We can create a list of Person objects and add six people like so:
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"));
C# 2.0's list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:
Console.WriteLine("Unsorted list");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
Console.WriteLine("Age is less than 25");
young.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
Console.WriteLine("Sorted list, by name");
people.Sort(delegate(Person p1, Person p2) { return p1.name.CompareTo(p2.name); });
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
people.Sort(delegate(Person p1, Person p2) { return p1.age.CompareTo(p2.age); });
Console.WriteLine("Sorted list, by age");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
And here is the output that we should expect:
Unsorted list 50 Fred 30 John 26 Andrew 24 Xavier 5 Mark 6 Cameron Age is less than 25 24 Xavier 5 Mark 6 Cameron Sorted list, by name 26 Andrew 6 Cameron 50 Fred 30 John 5 Mark 24 Xavier Sorted list, by age 5 Mark 6 Cameron 24 Xavier 26 Andrew 30 John 50 Fred
Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.
Related articles
Related discussion
-
how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
by vasanta (0 replies)
-
Help please for student
by mandy130 (0 replies)
-
Loop help needed
by BKRoberts (5 replies)
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Regarding User control creation in C# .Net(Windows Application)
by porchelvi (0 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
Thanks to all for the kind words!
Just wanted to say "thanks". That was the most elegant description for how to sort lists that I've seen.
Thanks for the code. This really helped me out.
Clear, concise. Really connected some dots for me. Thanks for posting.
Just wanted to say, yet simple, this is a wonderful article! Came upon it by chance. I havn't investigated Lists until now... thanks!!
-William,
This thread is for discussions of Sorting and Searching Using C# Generics .