Sorting Date in a Generic List using LINQ

Let us say you have a Generic List with you which contains a Date Field. You now want to sort Date in the Generic List and want to do it in the simplest possible way, then here's how to do so. I have added a DropDownList for demonstration purpose to display the results:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class LINQ : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)



        List<Employee> empList = new List<Employee>();

        empList.Add(new Employee() { ID = 1, FName = "John",  DOB = DateTime.Parse("12/11/1971")});

        empList.Add(new Employee() { ID = 2, FName = "Mary",  DOB = DateTime.Parse("01/17/1961")});

        empList.Add(new Employee() { ID = 3, FName = "Amber", DOB = DateTime.Parse("12/23/1971")});

        empList.Add(new Employee() { ID = 4, FName = "Kathy", DOB = DateTime.Parse("11/15/1976")});

        empList.Add(new Employee() { ID = 5, FName = "Lena",  DOB = DateTime.Parse("05/11/1978")});


        empList.Sort((x, y) => (x.DOB.CompareTo(y.DOB)));


        empList.ForEach(delegate(Employee em)







    class Employee


        public int ID { get; set; }

        public string FName { get; set; }

        public DateTime DOB { get; set; }





Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls



Partial Public Class LINQ

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


        Dim empList As New List(Of Employee)()

        empList.Add(New Employee() With {.ID = 1, .FName = "John", .DOB = DateTime.Parse("12/11/1971")})

        empList.Add(New Employee() With {.ID = 2, .FName = "Mary", .DOB = DateTime.Parse("01/17/1961")})

        empList.Add(New Employee() With {.ID = 3, .FName = "Amber", .DOB = DateTime.Parse("12/23/1971")})

        empList.Add(New Employee() With {.ID = 4, .FName = "Kathy", .DOB = DateTime.Parse("11/15/1976")})

        empList.Add(New Employee() With {.ID = 5, .FName = "Lena", .DOB = DateTime.Parse("05/11/1978")})


        empList.Sort(Function(x, y) (x.DOB.CompareTo(y.DOB)))


        empList.ForEach(AddressOf AnonymousMethod1)


    End Sub

    Private Sub AnonymousMethod1(ByVal em As Employee)


    End Sub


    Private Class Employee

        Private privateID As Integer

        Public Property ID() As Integer


                Return privateID

            End Get

            Set(ByVal value As Integer)

                privateID = value

            End Set

        End Property

        Private privateFName As String

        Public Property FName() As String


                Return privateFName

            End Get

            Set(ByVal value As String)

                privateFName = value

            End Set

        End Property

        Private privateDOB As DateTime

        Public Property DOB() As DateTime


                Return privateDOB

            End Get

            Set(ByVal value As DateTime)

                privateDOB = value

            End Set

        End Property

    End Class


End Class

Here we make use of the List.Sort to do an in place sorting.

After sorting the dates, the result will be as displayed below:

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Luke said...

Great post, was exactly what i was looking for.