AliBoronsi
AliBoronsi

Reputation: 764

Order list by special enum value in linq

i want to order my list according to below picture (first field is int and second is an Enum)

i want to records with "foo" value comes at first of output

how can i do this in c# and linq?

thanks in advance

public enum myEnum{
    goo,
    foo,
    boo
}

enter image description here

Upvotes: 11

Views: 9506

Answers (6)

ShawnFeatherly
ShawnFeatherly

Reputation: 2638

Possible updates to the Enum should be surfaced with a NotImplementedException. Which is easier to add if a function is used instead of the dictionary method @Dmitry mentioned.

private int SortOrderOf(myEnum enumValue)
{
    switch (enumValue)
    {
        case myEnum.foo: return 1;
        case myEnum.boo: return 2;
        case myEnum.goo: return 3;
        default: throw new NotImplementedException();
    }
}

usage:

var result = source
.OrderBy(item => SortOrderOf(item.SecondField)) // rely on SortOrderOf() to determine order of second field
.ThenBy(item => item.FirstField);

Using a function also allows for overloading with the same name. SortOrderOf() can be used for both myEnum and myEnumTwo. With a mapping dictionary different variable names would need to be used.

Upvotes: 0

Ali Can CUCU
Ali Can CUCU

Reputation: 1

var collection = new List<myCustomObjectWithEnumProperty>();
var sortedCollection = collection.OrderBy(c=>(int)c.EnumProperty);

To see: https://cmatskas.com/ordering-enums-with-c/

My Code:

public enum TicketPriorityEnum
{
    Düşük = 0,
    Normal = 1,
    Yüksek = 2,
}

if (priority == "3")
    d = d.OrderBy(x => (int)x.TicketPriority).ThenByDescending(x => x.Id);

if (priority == "4")
    d = d.OrderByDescending(x => (int)x.TicketPriority).ThenByDescending(x => x.Id);

Upvotes: -1

wkato
wkato

Reputation: 11

I found this reasonably simple as well:

var result = list.OrderBy(item => item.myEnum == myEnum.foo)
              .ThenBy(item => item.myEnum == myEnum.boo)
              .ThenBy(item => item.myEnum == myEnum.goo); 

Upvotes: 1

jbalintac
jbalintac

Reputation: 190

A simpler approach is to just evaluate the enum and return comparison value:

 var result = list.OrderBy(item => item.myEnum == myEnum.foo ? 1 : 2)
                  .ThenBy(item => item.myEnum == myEnum.boo ? 1 : 2)
                  .ThenBy(item => item.myEnum == myEnum.goo ? 1 : 2); 

This way you won't need extra variables and a more readable code.

Sample: https://repl.it/repls/CornsilkGiganticDevelopers

Upvotes: 5

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

For arbitrary enum and sort order e.g.

  public enum MyEnum {
    boo = -1,
    foo = 3, 
    goo = 154,    
  };

I suggest mapping:

  // we want foo, boo, goo order
  internal static readonly Dictionary<MyEnum, int> enumOrder = 
    new  Dictionary<MyEnum, int>() {
      {MyEnum.foo, 1},
      {MyEnum.boo, 2},
      {MyEnum.goo, 3},
  };

Query:

  var result = source
    .OrderBy(item => enumOrder[item.SecondField]) // map second field on the desired order
    .ThenBy(item => item.FirstField);      

Upvotes: 17

mjwills
mjwills

Reputation: 23898

The below code will sort yourData by two properties - NameOfYourEnumPropertyHere and NameOfYourIntPropertyHere. You need to replace with your property names and away you go.

var sortedResult = yourData
    .OrderBy(z => z.NameOfYourEnumPropertyHere)
    .ThenBy(z => z.NameOfYourIntPropertyHere).ToList();

Upvotes: 3

Related Questions