c# dictionary<> with keys custom order

C#
[Edit]
+
0
-
0

C# Dictionary<> with keys custom order

9600
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// ---------------------------------------- // Program.cs file: // ---------------------------------------- using System; using System.Collections.Generic; public class Program { public static void Main() { IDictionary<int, char> dictionary = new SortedDictionary<int, char>(new KeyComparer()); dictionary.Add(1, 'c'); dictionary.Add(2, 'b'); dictionary.Add(3, 'a'); foreach (KeyValuePair<int, char> pair in dictionary) Console.WriteLine(pair.Key + " " + pair.Value); } } // Output: // // 3 a // 2 b // 1 c // ---------------------------------------- // KeyComparer.cs file: // ---------------------------------------- using System.Collections.Generic; public class KeyComparer : IComparer<int> { public int Compare(int x, int y) { return y - x; } }