archangelmihail

SortedDictionaryByKeyByValue

Jan 19th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class SortedDictionaryByKeyByValue
  6. {
  7.     static void Main()
  8.     {
  9.         // Create a SortedDictionary with string key and Int16 value pair
  10.  
  11.         SortedDictionary<string, Int16> AuthorList = new SortedDictionary<string, Int16>();
  12.  
  13.  
  14.     dict = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  15.  
  16.         AuthorList.Add("Mahesh Chand", 35);
  17.  
  18.         AuthorList.Add("Mike Gold", 25);
  19.  
  20.         AuthorList.Add("Praveen Kumar", 29);
  21.  
  22.         AuthorList.Add("Raj Beniwal", 21);
  23.  
  24.         AuthorList.Add("Dinesh Beniwal", 84);
  25.  
  26.  
  27.  
  28.         // Sorted by Key
  29.  
  30.         Console.WriteLine("Sorted by Key");
  31.  
  32.         Console.WriteLine("=============");
  33.  
  34.         foreach (KeyValuePair<string, Int16> author in AuthorList.OrderBy(key => key.Key))
  35.         {
  36.  
  37.             Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
  38.  
  39.         }
  40.  
  41.         Console.WriteLine("=============");
  42.  
  43.         // Sorted by Value
  44.  
  45.         Console.WriteLine("Sorted by Value");
  46.  
  47.         Console.WriteLine("=============");
  48.  
  49.         foreach (KeyValuePair<string, Int16> author in AuthorList.OrderBy(key => key.Value))
  50.         {
  51.  
  52.             Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
  53.  
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment