Is there any way I can iterate backwards (in reverse) through a SortedDictionary in c#?
Or is there a way to define the SortedDictionary in descending order to begin
Briefly create a reversed sorted dictionary in one line.
var dict = new SortedDictionary(Comparer.Create((x, y) => y.CompareTo(x)));
There's a way to create a IComparer using System.Collections.Generic.Comparer. Just pass a IComparision delegate to its Create method to build a IComparer.
var dict = new SortedDictionary(
Comparer.Create(
delegate(int x, int y)
{
return y.CompareTo(x);
}
)
);
You can use a lambda expression/local function/method to replace the delegate if their significance are (TKey, TKey) => int.