3 ways to Sort in Descending order via lambdas

In C# , One can sort the List of Objects using Lambdas easily . Until now , i found the LINQ Query to be more easy to sort but after using the Lamba expression , I feel that the lambda expression can also be used effectively to sort in descending order .

For example , i use the below class for the data

public class Movie
{
   public string MovieName { get; set; }
   public string Actor { get; set; }
}
public class Movies : List<Movie>{
public Movies()
{  Add(new Movie { MovieName = "Endhiran", Actor = "Rajnikanth" });
   Add(new Movie { MovieName = "Velayutham", Actor = "Vijay" });
   Add(new Movie { MovieName = "Mankatha", Actor = "Ajith" });
   Add(new Movie { MovieName = "Nanban", Actor = "Vijay" });
   Add(new Movie { MovieName = "Kaavalan", Actor = "Vijay" });
}

}

To sort the Movie List in descending order by Actor , we can use the below LINQ Query

List movieList = new Movies();
var movies = (from movie in  movieList
orderby movie.Actor descending
select movie).ToList();
dataGridView1.DataSource = movies;

How do i use the Lambdas to sort the same list in descending order ? Its simple . There are different ways that one can use the lambdas to sort the list in descending .

3 ways to Sort in Descending order via lambdas in C#

1. Sorting the List in Ascending order via Lambda and then using the Reverse method of the List

List movieList = new Movies();
movieList.Sort((x, y) => x.Actor.CompareTo(y.Actor));
movieList.Reverse();
dataGridView1.DataSource = movieList;

2. Use the same sort method of the List but specify the minus symbol (-) while sorting

List movieList = new Movies();
movieList.Sort((x, y) => -x.Actor.CompareTo(y.Actor));
dataGridView1.DataSource = movieList;

When you use the + symbol instead of – , we get the list sorted in ascending order

3. Another simple way is to interchange the Comparer and CompareTo values like below

List movieList = new Movies();
movieList.Sort((x, y) => y.Actor.CompareTo(x.Actor));
dataGridView1.DataSource = movieList;

Definitely Learnt something today , seems like i need to learn more of the lambdas which i feel is simplifying lot of things 🙂

    6 Comments

  1. CSharp Questions
    September 11, 2011
    Reply

    This is just wrong.

    The .NET framework includes orderby descending so that you use it.
    Another programmer who reads your code will begin to wander why did you add that minus, or switched the order in the CompareTo variables, and what even worse, why did you add that extra line with .Reverse, when you could just used OrderByDescending.

    Never try to reinvent the wheel, if it has already been invented before, especially if it’s included in the .NET framework.

  2. September 13, 2011
    Reply

    Thanks for your feedback … The idea behind this is to explore other possibilities in .NET framework which u can use to sort . Definitely OrderBy and OrderByDescending is good . Also another point
    try the following
    List movieList = new Movies();
    movieList.OrderByDescending(a => a.MovieName).ToList();
    dataGridView1.DataSource = movieList;

    This will not wory and instead the below works … 🙂

    dataGridView1.DataSource = movieList.OrderByDescending(a => a.MovieName).ToList();

  3. Jon
    September 12, 2011
    Reply

    What about OrderBy and OrderByDescending LINQ extension methods?

  4. September 13, 2011
    Reply

    Yes OrderBy and OrderByDescending works … But i wanted to explore how one can do it with sort method 🙂 .

  5. d
    September 13, 2011
    Reply

    List movieList = new Movies();
    dataGridView1.DataSource = movieList.OrderByDescending(f=>f.Actor).ToLis();

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...