2

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 DescendingComparer<float>());

class DescendingComparer<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.

This is the code that accesses the dictionary:

foreach (KeyValuePair<float, foo> kv in sortedLayers)
{
    // ...
}

UPDATE: This works, but is too slow to call as frequently as I need to call this method:

IOrderedEnumerable<KeyValuePair<float, foo>> sortedLayers = layers.OrderByDescending(kv => kv.Key);
foreach (KeyValuePair<float, ICollection<IGameObjectController>> kv in sortedLayers) { 
    // ...
}

UPDATE: I put a break point in the comparator that never gets hit as I add and remove kv pairs from the dictionary. What could this mean?

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • 2
    I think you are confusing ascending with descending. Ascending is 1 2 3 4 5 (smallest value first), descending is 5 4 3 2 1 (largest value first). – dtb Apr 23 '10 at 22:41
  • `y.CompareTo(x)` should work for descending order. Why would it not? :o If you give few samples and output it will be easier to know. See [reverse-sorted-dictionary-in-net](http://stackoverflow.com/questions/931891/reverse-sorted-dictionary-in-net) – nawfal Jun 15 '14 at 10:28

3 Answers3

4

For descending order (largest values first), you would do -x.CompareTo(y)

Michael Petito
  • 12,891
  • 4
  • 40
  • 54
  • @Rosarch: It definitely should work, I know I've used it before. If you debug your code, do you find that your Compare method is actually called? – Michael Petito Apr 23 '10 at 22:46
  • Looks like it isn't, actually. Am I creating the `SortedDictionary` incorrectly? – Nick Heiner Apr 23 '10 at 22:48
  • 1
    @Rosarch: No, you are creating the dictionary correctly. Check to make sure that you are using the same dictionary you've constructed above later on in your code when adding/removing elements and enumerating. IE. Find all references to `layers` and make sure there isn't another piece of code assigning a different `SortedDictionary`. – Michael Petito Apr 23 '10 at 22:59
  • 2
    Negating can be a bad idea. If the `int` is the most negative value possible, `-2147483648`, then the negation of that is the same number, `-2147483648`. So it is better to swap `x` and `y`, as in `y.CompareTo(x)`, than to prepend a minus, as `-x.CompareTo(y)`. If you do not know if one of the comparands is `null`, you can use `Comparer.Default.Compare(y, x)`. – Jeppe Stig Nielsen Aug 09 '16 at 20:53
0

try:

public int Compare(T x, T y)
{
    return x.CompareTo(y);
}
derek
  • 4,826
  • 1
  • 23
  • 26
0

In this line, switch x and y:

return -y.CompareTo(x);

Make it

return -x.CompareTo(y);