title: OrderByDescending operates incorrectly due to integer overflow
body: <p>While browsing the .Net Framework source code for the Enumerable class it has occurred to me that its internal EnumerableSorter class <a href="http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,b388a66d2fbcb2a6" rel="nofollow noreferrer">CompareKeys method</a> used for sorting has a following line:</p>
  
  <pre><code>return descending ? -c : c;
  </code></pre>
  
  <p>Where <code>c</code> is the result of the <a href="https://msdn.microsoft.com/en-us/library/xh5ks3b3(v=vs.110).aspx" rel="nofollow noreferrer">IComparer.Compare Method (T, T)</a> method call, that doesn't actually force us to use only -1, 1 or 0 to denote ordering.</p>
  
  <p>Taking into account that <a href="https://stackoverflow.com/a/3622352/3745022"><code>-Int32.MinValue == Int32.MinValue</code></a> due to integer overflow, it can lead to incorrect sorting, as can be proved by the following snippet:</p>
  
  <pre><code>public class Value : IComparable&lt;Value&gt;
  {
      private readonly Int32 _value;
      public Value(Int32 value)
      {
          this._value = value;
      }
  
      public Int32 CompareTo(Value other)
      {
          if (other == null)
              throw new ArgumentNullException(nameof(other));
          var cmp = this._value.CompareTo(other._value);
          if (cmp &gt; 0)
              return Int32.MaxValue;
          if (cmp &lt; 0)
              return Int32.MinValue;
          return 0;
      }
  
      public override String ToString()
      {
          return this._value.ToString();
      }
  }
  
  private static void Print&lt;T&gt;(String header, IEnumerable&lt;T&gt; values)
  {
      Console.WriteLine(header);
      foreach (var item in values)
      {
          Console.WriteLine(item);
      }
      Console.WriteLine();
  }
  
  public static void Main()
  {
      try
      {
          var notSorted = new[] { 1, 3, 2 }
              .Select(i =&gt; 
                  new Value(i))
              .ToArray();
  
          Print("Not sorted", notSorted);
          Print("Sorted by", notSorted.OrderBy(item =&gt; item));
          Print("Sorted by descending", notSorted.OrderByDescending(item =&gt; item));
      }
      catch (Exception exc)
      {
          Console.WriteLine(exc);
      }
      Console.WriteLine("Press any key...");
      Console.ReadKey(true);         
  }
  </code></pre>
  
  <p>For <code>OrderByDescending</code> it produces:</p>
  
  <pre>
  Sorted by descending
  3
  1
  2
  </pre>
  
  <p>It was expected, but it is also an obviously incorrect result.</p>
  
  <p>So it seems to be a defect in the .Net, though a very unlikely to occur if <code>CompareTo</code> implemented in a reasonable way. Am I right?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As pointed by <a href="https://stackoverflow.com/users/34397/slaks">SLaks</a> the issue has been known for a long time but hasn't been fixed despite all the new releases - <a href="https://connect.microsoft.com/VisualStudio/feedback/details/634949/orderbydescending-fails-in-linq-to-objects-when-a-comparer-returns-int-minvalue" rel="nofollow noreferrer">https://connect.microsoft.com/VisualStudio/feedback/details/634949/orderbydescending-fails-in-linq-to-objects-when-a-comparer-returns-int-minvalue</a></p>
  
  <p>As pointed by <a href="https://stackoverflow.com/users/122718/usr">usr</a> .Net Core has this issue fixed - <a href="https://github.com/dotnet/corefx/blob/35e03c78d89d02f2d3b4a1f8b277a35c88f45750/src/System.Linq/src/System/Linq/OrderedEnumerable.cs#L628" rel="nofollow noreferrer">https://github.com/dotnet/corefx/blob/35e03c78d89d02f2d3b4a1f8b277a35c88f45750/src/System.Linq/src/System/Linq/OrderedEnumerable.cs#L628</a></p>