Sorting a list using Lambda/Linq to objects

This can be done as

list.Sort( (emp1,emp2)=>emp1.FirstName.CompareTo(emp2.FirstName) );

The .NET framework is casting the lambda (emp1,emp2)=>int as a Comparer<Employee>.

This has the advantage of being strongly typed.

If you need the descending/reverse order invert the parameters.

list.Sort( (emp1,emp2)=>emp2.FirstName.CompareTo(emp1.FirstName) );

Leave a Comment