private Dictionary<string, Dictionary<string, string>> sort(Dictionary<string, Dictionary<string, string>> prods)
{
int a, b;
var sortedDict =new Dictionary<string, Dictionary<string, string>>();
sortedDict = from entry in prods orderby entry.Value["SortOrder"] ascending select entry;
return sortedDict;
}
Error:
Error 4 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable>>' to 'System.Collections.Generic.Dictionary>'. An explicit conversion exists (are you missing a cast?)
You can use ToDictionary
to create a dictionary - but that won't actually achieve what you want.
A Dictionary<,>
simply isn't a sorted data structure. The ordering should not be relied on. Even though the current implementation may return entries in the order they were added, that's an implementation detail:
For purposes of enumeration, each item in the dictionary is treated as a
KeyValuePair<TKey, TValue>
structure representing a value and its key. The order in which the items are returned is undefined.
You might want to look at SortedDictionary<,>
, but given that the sort order appears to depend on the value rather than the key, it may well not be feasible.
Depending on what you're trying to do, you could potentially just declare that you return an IEnumerable<KeyValuePair<string, Dictionary<string, string>>
See more on this question at Stackoverflow