Suppose I have a dictionary<int, string> like this
dict1 = {{[4,bcefgh]},{[5,abcefgh]},{[6,efgh]},{[7,bcefgh]},{[10,cefghi]}}
I want to sort pairs in this dictionary according to length of string values, without using extra loops. That is, results should be:
dict1 = {{[6,efgh]},{[4,bcefgh]},{[7,bcefgh]},{[10,cefghi]},{[5,abcefgh]}}
My initial answer was to create a separate dictionary that had the same keys and length of every corresponding string and a third dictionary that loops over pair as follows:
foreach (KeyValuePair<int,string> pair in dict1)
{
temp_dict.Add(pair.Key, pair.Value.Count());
}
var items = from pair in temp_dict
orderby pair.Value ascending
select pair;
foreach (KeyValuePair<int, int> pair in items)
{
result_dict.Add(pair.Key, dict1[pair.Key]);
}
But this result is now practical with large set of data.