Created
August 27, 2020 19:16
-
-
Save danielsimionescu/8afdafd1292cee807e25f0f4eac53d45 to your computer and use it in GitHub Desktop.
C# Dictionaries - Sorting by Keys with Arrays
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.Generic; | |
using System.Linq; | |
namespace SortingDictionary | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var store = new Dictionary<string, double> | |
{ | |
["peach"] = 15, | |
["grape"] = 23, | |
["lemon"] = 8, | |
["coconut"] = 10 | |
}; | |
string[] sortedStoreKeys = store | |
.Keys | |
.ToArray(); | |
Array.Sort(sortedStoreKeys); | |
foreach (var key in sortedStoreKeys) | |
{ | |
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