Can I order an IEnumerable in C#, using "OrderBy()" and some boolean?

53 Views Asked by At

As mentioned in this other post, there are two ways to alter the direction of the ordering of a C# List (or any Enumerable):

  • use the OrderByDescending() method.
  • add .Reverse() at the end.

In my code, I have a lot of lines like these ones:

if (bDirection)
  list.OrderBy(...);
else
  list.OrderByDescending(...);

I would like to replace this by the following oneliner:

list.OrderBy(..., bDirection);

, where the boolean bDirection decides in which direction the ordering should be done.

Does this exist?

Thanks in advance

0

There are 0 best solutions below