Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# list sort by two columns

Tags:

I have a C# custom object list that I need to sort by two different variables one is a boolean and the other is a string. I can sort by either of the criteria, but I'm having trouble figuring out how to combine them. The sort should be all of the boolean values first (CheckedIn) and then the last name for each of the values. Right now I use

result.Sort((x, y) => string.Compare(x.CheckedIn.ToString(), y.CheckedIn.ToString()));
result.Sort((x, y) => string.Compare(x.LastName, y.LastName));

But how can I combine then so that my results are like

CheckedIn-Name
No - Aames
No - Smith
Yes - Barnes
Yes - Peters
like image 337
Unknown Coder Avatar asked Aug 17 '11 21:08

Unknown Coder


1 Answers

use linq.

if you have list L of objects of class

public class temp
{
public bool x;
public string y;
}

then use:

L.orderby(a=>a.x).thenby(a=>a.y);

you can chain it as far as you like.

like image 69
Tarang Avatar answered Nov 12 '22 10:11

Tarang