C# .NET: Ascending comparison of a SortedDictionary?
- by Rosarch
I'm want a IDictionary<float, foo> that returns the larges values of the key first.
private IDictionary<float, foo> layers = new SortedDictionary<float, foo>(new AscendingComparer<float>());
class AscendingComparer<T> : IComparer<T> where T : IComparable<T>
{
public int Compare(T x, T y)
{
return -y.CompareTo(x);
}
}
However, this returns values in order of the smallest first. I feel like I'm making a stupid mistake here.
Just to see what would happen, I removed the - sign from the comparator:
public int Compare(T x, T y)
{
return y.CompareTo(x);
}
But I got the same result. This reinforces my intuition that I'm making a stupid error.