Three Ways to Sort a List of Objects with DateTime in C#

Here are three quick ways to sort a List of objects that have DateTime properties in C#.

Initial Code:

public class Person{
    public string Name;
    public DateTime Birthday;

var people = new List<Person>();
var p1 = new Person() { Name = "Leslie", Birthday = new DateTime(1983, 9, 4) };
var p2 = new Person() { Name = "Chris", Birthday = new DateTime(2001, 6, 19) };
var p3 = new Person() { Name = "JP", Birthday = new DateTime(1983, 4, 5) };

people.Add(p1); people.Add(p2); people.Add(p3);
people.ForEach(p => Console.WriteLine(p.Name));

You can see that the output is: "Leslie", "Chris", and then "JP"

Let's start with the LINQ way:

var persons = from p in people
            orderby p.Birthday
            select p;
persons.ToList().ForEach(p => Console.WriteLine(p.Name));

You can see the correct output: "JP", "Leslie", and then "Chris"

Old fashion way using a Delegate:

people.Sort(delegate(Person ps1, Person ps2) { return DateTime.Compare(ps1.Birthday, ps2.Birthday); });
people.ForEach(p => Console.WriteLine(p.Name));

Cleaner way using a Lambda:

people.Sort((ps1, ps2) => DateTime.Compare(ps1.Birthday, ps2.Birthday));
people.ForEach(p => Console.WriteLine(p.Name));

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Read me blog on entrepreneurship: Techneur Follow me on Twitter: @jprichardson


If you made it this far, you should follow me on Twitter.


Want to test-drive Bitcoin without any risk? Check out my bitcoin wallet Coinbolt. It includes test coins for free.

comments powered by Disqus