Pages

Thursday, February 9, 2012

Order By Class List<> in C#

Order by Ascending and Descending class List<> in C# :

Introduction : In this article i will explain you how to sort class collection ( List<> of class ) in linq c# . I done the easy code to understand how to uses Order by Ascending and Descending of class list in linq c#.

Class example for sorting class List<> In c#: Following is the user class example with constructor in c#.
public class User
{
    public int UserId { get; set; }
    public string Firstname { get; set; }
    public User(int userId, string firstname)
    {
        UserId = userId;
        Firstname = firstname;
    }
}
Ascending and Descending Sorting class list in linq C#:
List<User> UserList = new List<User>(); 

UserList.Add(new User(1, "bimal"));
UserList.Add(new User(2, "punit"));
UserList.Add(new User(3, "amit"));

UserList = UserList.OrderBy(x => x.Firstname).ToList();  //Ascending Sorted the class list using linq.
UserList = UserList.OrderByDescending(x => x.Firstname).ToList();  // Descending Sorted the class list using linq.


Related Other posts

2 comments:

  1. First of all I want to say excellent blog! I had a quick question which I'd like to ask if you do not mind. I was interested to find out how you center yourself and clear your thoughts before writing. I have had a tough time clearing my mind in getting my thoughts out. I do take pleasure in writing however it just seems like the first 10 to 15 minutes are usually wasted simply just trying to figure out how to begin. Any recommendations or tips? Cheers!

    Look into my web site - www.livingwithrheumatoidarthritis.org

    ReplyDelete