Search code examples
c#sortingenums

C# Sort List by Enum


I have got a list of Entity, which has got an enum.

public class Car
{
    public int CarId { get; set; }

    public string CarName { get; set; }

    public CarCategory CarCategory { get; set; }
}

public enum CarCategory
{
    None = 0,
    kLowRange = 1,
    kMidRange = 2,
    kHighRange = 3
}

Now I have got a list of Cars, I would like to use Comparer and run it on the enum such that all the entity having CarCategory as kMidRange and kHighRange will be sorted to first in the list.

I have tried with the answer but havent found any luck.

Thanks.

UPDATE: I kinda have got the mistake I am doing. I was looking at

var sortedList = carList
  .OrderBy(x => x.CarCategory, 
                new EnumComparer<CarCategory> {
                  CarCategory.kMidRange, 
                  CarCategory.kHighRange});

But was getting only same values. I have to add .ToList() in order to get the result.

var sortedList = carList
  .OrderBy(x => x.CarCategory, 
                new EnumComparer<CarCategory> { 
                  CarCategory.kMidRange, 
                  CarCategory.kHighRange})
  .ToList();

Will give me the expected results. My mistake!


Solution

  • enum is effectively integer (int in your case)

    The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

    see https://msdn.microsoft.com/en-us/library/sbbt4032.aspx for details.

    Since CarCategory implementation has the items in the desired order you can put just

     var sortedList = carList
       .OrderByDescending(x => (int) (x.CarCategory))
       .ToList();
    

    please, note Descending: you want kHighRange to be on the top. If you want an arbitrary order, e.g.

    kMidRange, kHighRange, None, kLowRange
    

    I suggest using mapping:

     // desired order: None should be 3d, LowRange 4th, MidRange 1st, HighRange 2nd 
     int[] map = new[] {3, 4, 1, 2};
    
     var sortedList = carList
       .OrderBy(x => map[(int) (x.CarCategory)])
       .ToList();