How to convert a Dictionary to a SortedDictionary?
In addition to general conversion (preserving types of key and values) I’m interested in swapping the keys and values as part of the conversion: have a Dictionary<string, double> and I want to convert it to a SortedDictionary<double, string>.
How do I do this using LINQ extension methods in C# 3.0?
Why use LINQ? There is a constructor for this:
You could add a
ToSortedDictionary– but I wouldn’t bother…Note: this is an answer to the title of the question (convert a
Dictionaryto aSortedDictionaryfor the same types, if you need additional step that OP is looking for – swap key and values in the process – as shown in the updated problem, see this answer.