I have an Entity list that has an enumeration.
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 a list of cars, I would like to use Comparer and run it on an enumeration so that the whole object having a CarCategory like kMidRange and kHighRange will be sorted first in the list.
I tried with the answer, but havent found any luck.
Thanks.
UPDATE: I have a mistake that I am making. I looked at
var sortedList = carList .OrderBy(x => x.CarCategory, new EnumComparer<CarCategory> { CarCategory.kMidRange, CarCategory.kHighRange});
But only the same values ββwere obtained. I have to add .ToList () to get the result.
var sortedList = carList .OrderBy(x => x.CarCategory, new EnumComparer<CarCategory> { CarCategory.kMidRange, CarCategory.kHighRange}) .ToList();
Give me the expected results. To blame!
source share