Ordering list by many conditions using Linq
c#, linq, sorting
Solution
You can use `OrderBy` to order `B` first, the trick in here is `false` always comes before `true`. Then you order by `Age`:
var result = list.OrderBy(p => p.Group != "B")
.ThenBy(p => p.Age);
Problem
I have the code below that fills the list. ``` List<Person> people = new List<Person>(); for (int i = 1; i <= 30; i++) { var p = new Person(); if (i <= 10) p.Group = "A"; else if (i <= 20) p.Group = "B"; else p.Group = "C"; p.Name = "Person " + i; if (i % 3 == 0) p.Age = 10; else p.Age = 20; people.Add(p); } ``` Now I want to do sort in following order: All the people must be first that has Group "B" and must be in order by age. Then Rest of the people must be in order by Age, Group doesn't have to be in order. Here is the example: ``` Name Group Age Person B 10 Person B 10 ..... Person B 20 Person B 20 Person B 20 ..... //From here Group doesn't have to be in order. Person A 10 Person A 10 ..... Person C 10 Person C 10 ..... Person A 20 Person A 20 Person A 20 ..... Person C 20 Person C 20 Person C 20 .... ``` Can I do this with one linq query? Thanks for the help!