Home > Mobile >  How to sort use thenBy correctly c# linq?
How to sort use thenBy correctly c# linq?

Time:10-01

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?

CodePudding user response:

This works for me:

var priority = new [] { "London", "Paris", "Berlin" };

var list2 =
    list
        .OrderBy(x =>
            x.VisitPlace == null
            ? priority.Length
            : Array.IndexOf(priority, x.VisitPlace.Split(';')[0]))
        .ToList();

It gives:

list2

A more robust version is this:

var list2 =
(
    from person in list
    let firstVisitPlace =
        person.VisitPlace == null
        ? null
        : person.VisitPlace.Split(';')[0]
    let position =
        priority.Contains(firstVisitPlace)
        ? Array.IndexOf(priority, firstVisitPlace)
        : priority.Length
    orderby position
    select person
).ToList();

CodePudding user response:

This works for me:

I would move the logic to a dedicated method, like this:

foreach (var item in people.OrderBy(MyMagicOrder))
{
    Console.WriteLine($"{item.FirstName} | {item.VisitPlace}");
}

int MyMagicOrder(Person p)
{
    if (string.IsNullOrEmpty(p.VisitPlace))
        return 4;
    
    var split = p.VisitPlace.Split(";", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
    var firstVisitPlace = split.First();
    if (firstVisitPlace == "London")
        return 0;
    if (firstVisitPlace == "Paris")
        return 1;
    if (firstVisitPlace == "Berlin")
        return 2;
    
    return 3;
}

Here you can see a working solution:

https://dotnetfiddle.net/50kYS2

CodePudding user response:

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();
  • Related