1

I have a method which sorts a List of Clients.

There are two parameters I want to sort by. I am well aware of the

List.OrderBy(x => x. [...]).ThenBy... 

Method but my situation is a bit different:

switch(mySortType)
{
     case SortType.mostVisits:
         clients = clients.OrderBy(x => x. and so on...)
     break;
     More cases with different sortTypes
}

And after that I would like to sort after a Boolean parameter, but only if the settings tell the method to do so via another bool. Ist there a way to say something like

clients = clients.OrderBy(Current).ThenBy(x => x.param).ToList()?

I could of course have an if() in every case

case SortType.MostVisits:
  if(settingsTellsMeToDoSo)
     clients = clients.OrderBy(x=>x.numberOfVisits).ThenBy(x => x.param).ToList();
  else clients = clients.OrderBy(x => x.numberOfVisits).ToList();

and then use the simple ThenBy Method but I thaught that there has to be an easier way to do it.

I hope you got my question right. I am not sure whether or not I was able to explain well enough...

regards, Eric

2 Answers 2

2

Since the problem is to decide which lambda to put into OrderBy (as opposed to the rest of the lambdas, which go into ThenBy) you can use a little trick, and put all your lambdas (or no lambdas if no sorting is to be performed) into ThenBy. Since ThenBy is an extension method on IOrderedEnumerable<T>, you need a way to make a regular enumerable into an ordered enumerable without disturbing the original order.

You can make an IOrderedEnumerable<T> from your IEnumerable<T> (or IOrderedQueryable<T> from IQueryable<T>, depending on your situation) by applying a dummy sort on a constant, like this:

IOrderedEnumerable<Client> orderdClients = list.OrderBy(c => 1);

Now you can apply ThenBy repeatedly as needed. This approach is similar in nature to making a dummy search condition when dealing with a chain of AND operations which may contain zero elements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the hint List.OrderBy(c => 1). Did not know about that! Both your answers helped me to fix my problem. Which one should i mark as an answer?
1

I am not sure what you are trying to simplify, you can't remove the decision making from your code, but you could order the sort statements better:

IOrderedEnumerable<Client> orderedClients;

case SortType.MostVisits:
  orderedClients = clients.OrderBy(x => x.numberOfVisits);
  if(settingsTellsMeToDoSo)
      orderedClients = orderedClients.ThenBy(x => x.param);
break;

then after the switch is ended:

clients = orderedClients.ToList();

4 Comments

I was unaware that it was possible to call ThenBy() without a OrderBy directly in front of it, since I always called .ToList() after the first ordering... But without that, your way should work!
What i meant by simplifying is to split that ordering into two seperate parts in order to avoid a few if-clauses... Thank you for helping! your answer helped!
Glad it helped. Looking more closely I do think you need an intermediate variable to hold the result from OrderBy.
yes that is true. I used a IEnumerable variable to store the filtered (.Where()) and then "converted" that into an IOrderedEnumerable with var.OrderBy(x => 1), and then applied my second sort-parameter. But thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.