Commit f347cf61 authored by Marc Gravell's avatar Marc Gravell

fix relative sorting of values

parent 75d1257d
......@@ -104,6 +104,21 @@ public bool IsNullOrEmpty
}
}
// if either is a numeric type, and the other isn't the *same* type (above), then:
// it can't be equal
switch (xType)
{
case StorageType.Int64:
case StorageType.Double:
return false;
}
switch (yType)
{
case StorageType.Int64:
case StorageType.Double:
return false;
}
// otherwise, compare as strings
return (string)x == (string)y;
}
......@@ -292,33 +307,47 @@ internal StorageType Type
/// Compare against a RedisValue for relative order
/// </summary>
/// <param name="other">The other <see cref="RedisValue"/> to compare.</param>
public int CompareTo(RedisValue other)
public int CompareTo(RedisValue other) => CompareTo(this, other);
static int CompareTo(RedisValue x, RedisValue y)
{
try
{
StorageType thisType = this.Type,
otherType = other.Type;
x = x.Simplify();
y = y.Simplify();
StorageType xType = x.Type, yType = y.Type;
if (thisType == StorageType.Null) return otherType == StorageType.Null ? 0 : -1;
if (otherType == StorageType.Null) return 1;
if (xType == StorageType.Null) return yType == StorageType.Null ? 0 : -1;
if (yType == StorageType.Null) return 1;
if (thisType == otherType)
if (xType == yType)
{
switch (thisType)
switch (xType)
{
case StorageType.Double:
return this.OverlappedValueDouble.CompareTo(other.OverlappedValueDouble);
return x.OverlappedValueDouble.CompareTo(y.OverlappedValueDouble);
case StorageType.Int64:
return this._overlappedValue64.CompareTo(other._overlappedValue64);
return x._overlappedValue64.CompareTo(y._overlappedValue64);
case StorageType.String:
return string.CompareOrdinal((string)this._objectOrSentinel, (string)other._objectOrSentinel);
return string.CompareOrdinal((string)x._objectOrSentinel, (string)y._objectOrSentinel);
case StorageType.Raw:
return this._memory.Span.SequenceCompareTo(other._memory.Span);
return x._memory.Span.SequenceCompareTo(y._memory.Span);
}
}
switch (xType)
{ // numbers can be compared between Int64/Double
case StorageType.Double:
if (yType == StorageType.Int64) return x.OverlappedValueDouble.CompareTo((double)y._overlappedValue64);
break;
case StorageType.Int64:
if (yType == StorageType.Double) return ((double)x._overlappedValue64).CompareTo(y.OverlappedValueDouble);
break;
}
// otherwise, compare as strings
return string.CompareOrdinal((string)this, (string)other);
return string.CompareOrdinal((string)x, (string)y);
}
catch (Exception ex)
{
......@@ -693,7 +722,7 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
/// </summary>
private RedisValue Simplify()
{
switch(Type)
switch (Type)
{
case StorageType.String:
string s = (string)_objectOrSentinel;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment