Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class SortedDictionaryByKeyByValue
- {
- static void Main()
- {
- // Create a SortedDictionary with string key and Int16 value pair
- SortedDictionary<string, Int16> AuthorList = new SortedDictionary<string, Int16>();
- dict = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
- AuthorList.Add("Mahesh Chand", 35);
- AuthorList.Add("Mike Gold", 25);
- AuthorList.Add("Praveen Kumar", 29);
- AuthorList.Add("Raj Beniwal", 21);
- AuthorList.Add("Dinesh Beniwal", 84);
- // Sorted by Key
- Console.WriteLine("Sorted by Key");
- Console.WriteLine("=============");
- foreach (KeyValuePair<string, Int16> author in AuthorList.OrderBy(key => key.Key))
- {
- Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
- }
- Console.WriteLine("=============");
- // Sorted by Value
- Console.WriteLine("Sorted by Value");
- Console.WriteLine("=============");
- foreach (KeyValuePair<string, Int16> author in AuthorList.OrderBy(key => key.Value))
- {
- Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment