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 with?
.netc++dictionaryiterationreverse
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 with?
Best Solution
The SortedDictionary itself doesn't support backward iteration, but you have several possibilities to achieve the same effect.
Use
.Reverse
-Method (Linq). (This will have to pre-compute the whole dictionary output but is the simplest solution)Make the dictionary sort in descending order.
Use
SortedList<TKey, TValue>
instead. The performance is not as good as the dictionary's (O(n) instead of O(logn)), but you have random-access at the elements like in arrays. When you use the generic IDictionary-Interface, you won't have to change the rest of your code.Edit :: Iterating on SortedLists
You just access the elements by index!