ordering with lambda

  • Thread starter Thread starter Peter K
  • Start date Start date
P

Peter K

Hi, I have a list of user objects which I want to sort using a lambda
expression.

I can use "OrderBy" like this:

IList<IUser> orderedList = users.OrderBy(u=>u.Surname).ToList();

where I am sorting the list by the users' surnames.

But what if I want to "sub sort" by firstname, how do I do that? I mean,
there could be several users with the same surname, and I want to have all
the users with the same surname sorted according to their firstnames.


Thanks,
Peter
 
Peter said:
Hi, I have a list of user objects which I want to sort using a lambda
expression.

I can use "OrderBy" like this:

IList<IUser> orderedList = users.OrderBy(u=>u.Surname).ToList();

where I am sorting the list by the users' surnames.

But what if I want to "sub sort" by firstname, how do I do that? I mean,
there could be several users with the same surname, and I want to have all
the users with the same surname sorted according to their firstnames.

Use ThenBy
IList<IUser> orderedList =
users
.OrderBy(u=>u.Surname)
.ThenBy(u=>u.Firstname)
.ToList();
 
Back
Top