Search code examples
c#listlinqsorting

How to sort use thenBy correctly c# linq?


I have class Person

public class Person 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string VisitPlace { get; set; } 
}

I have list of this class

List<Person> list = new List<Person>()
{
    new Person { FirstName = "Name1", LastName = "Smith", VisitPlace = "London; Paris" },
    new Person { FirstName = "Name2", LastName = "Smith", VisitPlace = "Berlin" },
    new Person { FirstName = "Name3", LastName = "Smith", VisitPlace = "London; Berlin" },
    new Person { FirstName = "Name4", LastName = "Smith", VisitPlace = "Berlin" },
    new Person { FirstName = "Name5", LastName = "Smith", VisitPlace = null },
    new Person { FirstName = "Name6", LastName = "Smith", VisitPlace = "Paris" },
    new Person { FirstName = "Name7", LastName = "Smith", VisitPlace = null },
    new Person { FirstName = "Name8", LastName = "Smith", VisitPlace = "Paris; London" },
    new Person { FirstName = "Name9", LastName = "Smith", VisitPlace = "London" },
};

I want to sort this list:

  • Person who visitPlace London

  • Person who visitPlace Paris

  • Person who visitPlace Berlin (If person have two visitPlace for example (VisitPlace = London;Paris) then important is just first place)

  • all person with visitPlace = null - in the end of the list

var list2 = list.OrderByDescending(x => x.VisitPlace).ThenByDescending(x => x.VisitPlace == "London").ThenBy(x => x.VisitPlace == "Paris").ThenBy(x => x.VisitPlace == "Berlin");

Correct person in list: Person1, Person3, Person9, Person6, Person8, Person2, Person4, Person5, Person7

It is sorted but only just orderbyDescending. ThenBy doesnt work.

Why thenBy doesnt work? How can I sort it correctly with linq?


Solution

  • This works for me:

    var list2 = list
        .OrderBy(x => x.VisitPlace?.Split(';').First() == "London")
        .ThenBy(x => x.VisitPlace?.Split(';').First() == "Paris")
        .ThenBy(x => x.VisitPlace?.Split(';').First() == "Berlin")
        .ThenByDescending(x => x.FirstName)
        .Reverse()
        .ToList();