@arAABAXZCC| 6588465764VB 68849646VZYCWB 595VBCZP5VZ-

Sort Dictionary.` Dictionary has no Sort method. If we need to loop through the Dictionary contents in sorted order, we must separately acquire the elements and sort them.`Sorting approaches.` We can sort a Dictionary with the Keys and Values properties and a List instance. Some approaches are slower than others.`An example.` An example Dictionary is created. We apply the Keys property on the Dictionary instance. Then we use the ToList extension method and the Sort instance method. `ToList `tolist`Next: `The ToList and Sort methods are used on the Keys. The resulting List is looped through using the foreach-loop construct.`The var implicit typed keyword is used throughout to reduce syntactic redundancy.`Sort values.` This program adds keys to a Dictionary and then sorts them by their values. Dictionary instances are not initially sorted. We use the orderby keyword in a query statement. `orderby `orderby`Notes, above program.` First it declares an example Dictionary. The Dictionary keys are strings. Its values are ints. We will reorder the values to go from lowest to highest.`Notes, continued.` It accesses the KeyValuePairs in a query. It displays results, using foreach to iterate through and display each KeyValuePair. No additional lookups are required. `LINQ `linq`Var `var`KeyValuePair `keyvaluepair`Thanks to Jon Senchyna for improving the query expression. It is more efficient to use the collection of KeyValuePairs.`OrderBy.` Another option is the OrderBy extension method in System.Linq. This approach compiles to the same code as the orderby query expression. `Here the syntax is shorter. It requires only one lambda expression and method call.`Lambdas `lambda`Notes, above program.` The program could, with certain changes, raise a KeyNotFoundException. You will want to trap those errors with exception handling—try and catch. `Exception `exception`Thanks to Even Holen for writing in with an example of the shorter OrderBy syntax form.`Tip, opposite direction.` It is possible and easy to sort in the opposite direction. Replace ascending with descending. When you omit the direction keyword, it will use ascending. `Descending `descending`Notes, other methods.` Other methods I found involve more steps, code or complexity. There is nothing wrong with those methods, although they vary in efficiency.`Sort string values.` Sorting strings would work just as well. Sort() uses the interface implementations of the types. Its syntax is the same for any type that List.Sort() could sort. `IComparable `icomparable`A summary.` Dictionary is optimized for fast lookups, not for specific looping mechanisms. It helps in lookup-heavy programs, but hinders programs that require sorted keys.

467 6P6O; 6 6P6O.Linq6S6D6; { 46$4{7 446{C6` d6+6Radd five keys6R6ds. 447var d6+6z6w7D6+7<6L, 6}>(); 44d6+.6n7"car"7, 2); 44d6+.6n7"apple"7, 1); 44d6+.6n7"zebra"7, 0); 44d6+.6n7"mouse"7, 5); 44d6+.6n7"year"7, 3);7 446{Acquire keys6Rsort them. 447var 6t6zd6+.Keys.7To6u7(); 446t.7Sort7();7 446{6]through keys. 44767 (var key6k6t) 44{6'6%7"{0}: {1}"7, key, d6+[key]); 44} 4} } 7 apple: 1 car: 2 mouse: 5 year: 3 zebra: 07 6P6O; 6 6P6O.Linq6S6D6; { 46$4{7 446{E6Q d6+. 447var d6+6z6wD6+<6L, 6}>(5); 44d6+.6n7"cat"7, 1); 44d6+.6n7"dog"7, 0); 44d6+.6n7"mouse"7, 5); 44d6+.6n7"eel"7, 3); 44d6+.6n7"programmer"7, 2);7 446{Order by 6ds. 4462Use LINQ6lspecify sorting by 6d. 447var items6zfrom pair6kd6+6'447orderby7 pair.6e ascending6'44select pair;7 446{65 6Is. 44767 (Key6ePair<6L, 6}> pair6kitems) 44{6'6%7"{0}: {1}"7, pair.Key, pair.6e); 44}7 446{Reverse sort. 4462Can be looped over6kthe same way as above. 447items6zfrom pair6kd6+6'4orderby pair.6e descending6'4select pair; 4} } 7 dog: 0 cat: 1 programmer: 2 eel: 3 mouse: 57 6P6O; 6 6P6O.Linq6S6D6; { 46$4{ 44var items6z6w7D6+7<6}, 6}>(); 44items.6n-1, 0); 44items.6n0, 1); 44items.6n-2, 0); 44items.6n3, 1);7 446{Use OrderBy m6[. 44767 (var item6kitems.7OrderBy7(i 7=>7 i.Key)) 44{6'6%item); 44} 4} } 7 [-2, 0] [-1, 0] [0, 1] [3, 1]7Descending sort:7 var items6zfrom pair6kd6+6'orderby pair.6e 7descending76'select pair6S7 mouse: 5 eel: 3 programmer: 2 cat: 1 dog: 07

8sorts keys in Dictionary0sorts Dictionary^OrderBy method