title: C# Sort List by Enum
body: <p>I have got a list of Entity, which has got an enum. </p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I have tried with the <a href="https://stackoverflow.com/a/17309576">answer</a> but havent found any luck.</p>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE:</strong>
  I kinda have got the mistake I am doing. I was looking at </p>
  
  <pre><code>var sortedList = carList
    .OrderBy(x =&gt; x.CarCategory, 
                  new EnumComparer&lt;CarCategory&gt; {
                    CarCategory.kMidRange, 
                    CarCategory.kHighRange});
  </code></pre>
  
  <p>But was getting only same values. I have to add .ToList() in order to get the result.</p>
  
  <pre><code>var sortedList = carList
    .OrderBy(x =&gt; x.CarCategory, 
                  new EnumComparer&lt;CarCategory&gt; { 
                    CarCategory.kMidRange, 
                    CarCategory.kHighRange})
    .ToList();
  </code></pre>
  
  <p>Will give me the expected results. My mistake! </p>