Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# order list by two different things

Tags:

c#

list

.net-4.0

Let's say I have the following list:

var someList = new List<SomeObject>();

Which has the following entries:

SomeObject.Field

Apple
Orange
FruitBowl_Outside
Banana
Grape
FruitBowl_Inside

I would like to sort this list so that FruitBowl entries go at the bottom, and that subject to this, everything is alphabetic.

In order words:

SomeObject.Field

Apple
Banana
Grape
Orange
FruitBowl_Inside
FruitBowl_Outside
like image 262
Karl Avatar asked Jul 07 '11 06:07

Karl


1 Answers

You can use the OrderBy() and ThenBy() methods:

var orderedList = someList.OrderBy(obj => obj.Field.StartsWith("FruitBowl_"))
                          .ThenBy(obj => obj.Field);
like image 103
Frédéric Hamidi Avatar answered Sep 30 '22 00:09

Frédéric Hamidi