Created
August 17, 2020 09:27
-
-
Save danielsimionescu/8dcd14a53ad62b00063a1ef0df35ae9e to your computer and use it in GitHub Desktop.
SortingDictionaries.Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace SortingDictionaries | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var store = new Dictionary<string, double> | |
{ | |
["peach"] = 8, | |
["grape"] = 23, | |
["lemon"] = 8, | |
["coconut"] = 10 | |
}; | |
string[] sortedStoreByKey = store | |
.Keys | |
.ToArray(); | |
Array.Sort(sortedStoreByKey); | |
foreach (var key in sortedStoreByKey) | |
{ | |
Console.WriteLine($"{key}: {store[key]}"); | |
} | |
Console.WriteLine("...."); | |
foreach (var item in store) | |
{ | |
Console.WriteLine($"{item.Key}: {item.Value}"); ; | |
} | |
Console.WriteLine("..."); | |
string[] sortedStoreByValue = store | |
.OrderBy(item => item.Value) | |
.Select(item => item.Key) | |
.ToArray(); | |
foreach (var key in sortedStoreByValue) | |
{ | |
Console.WriteLine($"{key}: {store[key]}"); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment