Skip to content

Instantly share code, notes, and snippets.

@danielsimionescu
Created August 17, 2020 09:27
Show Gist options
  • Save danielsimionescu/8dcd14a53ad62b00063a1ef0df35ae9e to your computer and use it in GitHub Desktop.
Save danielsimionescu/8dcd14a53ad62b00063a1ef0df35ae9e to your computer and use it in GitHub Desktop.
SortingDictionaries.Program
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