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!