You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int.MinValue is of course traditionally considered less than 0. It is therefore a valid "x is less than y" return value for IComparer<T>.Compare(T x, T y).
But if an implementation returns it when it is correct, then Enumerable.OrderByDescending will treat it indicating that y is greater than x.
Demonstrating test case:
private class ExtremeComparer : IComparer<int>
{
public int Compare(int x, int y)
{
if (x == y)
return 0;
if (x < y)
return int.MinValue;
return int.MaxValue;
}
}
[Fact]
public void OrderByExtremeComparer()
{
var outOfOrder = new[] { 7, 1, 0, 9, 3, 5, 4, 2, 8, 6 };
Assert.Equal(Enumerable.Range(0, 10), outOfOrder.OrderBy(i => i, new ExtremeComparer()));
Assert.Equal(Enumerable.Range(0, 10).Reverse(), outOfOrder.OrderByDescending(i => i, new ExtremeComparer()));
}
The second assertion fails, as rather than the expected 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, the result is 3, 5, 1, 4, 9, 2, 0, 8, 7, 6.
The text was updated successfully, but these errors were encountered:
int.MinValue is a valid "x is less than y" result for a comparer but
OrderByDescending treats it incorrectly. Fix this.
Fix #2239
Simplify comparison result inverting.
As per suggestion at
dotnet#2240 (comment)
Simpler expression.
As suggested at dotnet#2240 (comment)
This reduces descending fix to a single branch in all cases.
Parentheses to clarify precedence in logic.
Replace boolean ^ with !=
Logically equivalent, and arguably better known.
Silly typo in comment.
int.MinValue is a valid "x is less than y" result for a comparer but
OrderByDescending treats it incorrectly. Fix this.
Fix #2239
Simplify comparison result inverting.
As per suggestion at
dotnet#2240 (comment)
Simpler expression.
As suggested at dotnet#2240 (comment)
This reduces descending fix to a single branch in all cases.
Parentheses to clarify precedence in logic.
Replace boolean ^ with !=
Logically equivalent, and arguably better known.
Silly typo in comment.
int.MinValue
is of course traditionally considered less than 0. It is therefore a valid "x is less than y" return value forIComparer<T>.Compare(T x, T y)
.But if an implementation returns it when it is correct, then
Enumerable.OrderByDescending
will treat it indicating thaty
is greater thanx
.Demonstrating test case:
The second assertion fails, as rather than the expected
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
, the result is3, 5, 1, 4, 9, 2, 0, 8, 7, 6
.The text was updated successfully, but these errors were encountered: