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
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by Paul2010 (26 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by DrFed (101 replies)
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by DrFed (13 replies)
-
Buy Valium amex online. Valium online fed ex. Valium no prior prescription. Valium and price.
by DrFed (11 replies)
-
Not expensive order prescription Diazepam. Overnight delivery of Diazepam in US no prescription needed. Buy generic Diazepam. Cheap Diazepam no prescription next day delivery.
by DrFed (11 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
Excellent synopsis.
!--removed tag-->Simplest way to explain Sorting in List ..
Thanks!!
!--removed tag-->I second that. Straight + nice: Perfect!
tnx, zara
!--removed tag-->This was exactly what I was looking for... thanks for the posting!
!--removed tag-->Clear, concise. Really connected some dots for me. Thanks for posting.
-William,
This thread is for discussions of Sorting and Searching Using C# Generics .