Skip to content

Instantly share code, notes, and snippets.

@danielsimionescu
Created August 27, 2020 19:16
Show Gist options
  • Save danielsimionescu/8afdafd1292cee807e25f0f4eac53d45 to your computer and use it in GitHub Desktop.
Save danielsimionescu/8afdafd1292cee807e25f0f4eac53d45 to your computer and use it in GitHub Desktop.
C# Dictionaries - Sorting by Keys with Arrays
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