-2

Here key is a string and value is a list of string. The longest list should come first and shortest at last.

Dictionary<string, List<string>> InputDictionary = {‘A’:[A1, A2, A3, A4], ‘B’:[B1, B2], ‘C’:[C1, C2, C3]}

Output has to be

output = {‘A’:[A1, A2, A3, A4], ‘C’:[C1, C2, C3], ‘B’:[B1, B2]} based on value length.

Please reply.

Richard
  • 106,783
  • 21
  • 203
  • 265

1 Answers1

1

This should do it for you.

Dictionary<char, List<string>> input = new Dictionary<char, List<string>>();
input.Add('A', new List<string>() { "A1", "A2", "A3", "A4" });
input.Add('B', new List<string>() { "B1", "B2", "B3", "B4", "B5" });
input.Add('C', new List<string>() { "C1", "C2", "C3" });
input.Add('D', new List<string>() { "D1", "D2" });

input = input.OrderByDescending(x => x.Value.Count).ToDictionary(x => x.Key, x => x.Value);
Ehsan
  • 31,833
  • 6
  • 56
  • 65