C# .NET: Ascending comparison of a SortedDictionary?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-04-23T22:31:04Z Indexed on 2010/04/23 22:33 UTC
Read the original article Hit count: 612

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET