From 0aeb39e31a6659eb18dd2357c5ce480f417930e5 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 1 Nov 2026 13:23:59 +0100 Subject: [PATCH] Sort Dictionary by Value: move to .NET 10, fix test asserts, add printed samples Retarget the three projects to net10.0 and update BenchmarkDotNet, xunit, xunit.runner.visualstudio, Microsoft.NET.Test.Sdk and coverlet.collector. Replace Assert.Same string checks with Assert.Equal against one expected array, add the ThenBy tie-break, SortedDictionary and OrderedDictionary samples with a test each, and return the sorted list from the benchmark methods. --- .../SortDictionaryByValue.Benchmark.csproj | 4 +- .../SortDictionaryByValueBenchMark.cs | 12 +-- .../SortDictionaryByValue/SortDictionary.cs | 26 +++++- .../SortDictionaryByValue.csproj | 2 +- .../Tests/SortDictionaryByValueTests.csproj | 10 +-- .../Tests/SortDictionaryByValueUnitTest.cs | 82 +++++++++++-------- 6 files changed, 88 insertions(+), 48 deletions(-) diff --git a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValue.Benchmark.csproj b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValue.Benchmark.csproj index 2bcc23ba92..7c7f9839ff 100644 --- a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValue.Benchmark.csproj +++ b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValue.Benchmark.csproj @@ -2,13 +2,13 @@ Exe - net6.0 + net10.0 enable enable - + diff --git a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValueBenchMark.cs b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValueBenchMark.cs index 9fcb7518d7..c6bd00a0f7 100644 --- a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValueBenchMark.cs +++ b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValueBenchMark.cs @@ -12,22 +12,22 @@ public IEnumerable DictionaryInput() [Benchmark] [ArgumentsSource(nameof(DictionaryInput))] - public void SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput) + public List> SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput) { - SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary); + return SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary); } [Benchmark] [ArgumentsSource(nameof(DictionaryInput))] - public void SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput) + public List> SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput) { - SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary); + return SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary); } [Benchmark] [ArgumentsSource(nameof(DictionaryInput))] - public void SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput) + public List> SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput) { - SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary); + return SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary); } } diff --git a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionary.cs b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionary.cs index 9ec388d2b7..53aff0e4fe 100644 --- a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionary.cs +++ b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionary.cs @@ -26,4 +26,28 @@ public static List> SortDictionaryValueUsingSortMet return sortedKeyValuePairs; } -} \ No newline at end of file + + public static List> SortDictionaryValueThenKey(Dictionary dictionary) + { + var sortedKeyValuePairs = dictionary + .OrderBy(x => x.Value) + .ThenBy(x => x.Key) + .ToList(); + + return sortedKeyValuePairs; + } + + public static SortedDictionary SortDictionaryKeyUsingSortedDictionary(Dictionary stock) + { + var sortedStock = new SortedDictionary(stock); + + return sortedStock; + } + + public static OrderedDictionary KeepValueOrderInOrderedDictionary(Dictionary dictionary) + { + var orderedByValue = new OrderedDictionary(dictionary.OrderBy(x => x.Value)); + + return orderedByValue; + } +} diff --git a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionaryByValue.csproj b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionaryByValue.csproj index 9a77342a90..00bac24b42 100644 --- a/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionaryByValue.csproj +++ b/collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionaryByValue.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 enable enable diff --git a/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueTests.csproj b/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueTests.csproj index d271f6f224..1fbdda0cc1 100644 --- a/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueTests.csproj +++ b/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueTests.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueUnitTest.cs b/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueUnitTest.cs index 77f4c80517..a4f25451db 100644 --- a/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueUnitTest.cs +++ b/collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueUnitTest.cs @@ -5,60 +5,76 @@ namespace SortDictionaryByValueTests; public class SortDictionaryByValueUnitTest { + private static Dictionary CreateDictionary() => new() + { + { "1", "apple" }, + { "2", "orange" }, + { "3", "banana" }, + { "4", "grape" } + }; + + private static readonly string[] ExpectedValues = ["apple", "banana", "grape", "orange"]; + [Fact] public void GivenUnorderedDictionary_WhenSortedUsingLinqOrderBy_ThenReturnOrderedKeyValuePairs() { - var dictionary = new Dictionary - { - { "1", "apple" }, - { "2", "orange" }, - { "3", "banana" }, - { "4", "grape" } - }; + var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqOrderBy(CreateDictionary()); - var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionary); - - Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple"); - Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana"); - Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape"); - Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange"); + Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value)); } [Fact] public void GivenUnorderedDictionary_WhenSortedUsingSortMethod_ThenReturnOrderedKeyValuePairs() + { + var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingSortMethod(CreateDictionary()); + + Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value)); + } + + [Fact] + public void GivenUnorderedDictionary_WhenSortedUsingLinqQueryExpression_ThenReturnOrderedKeyValuePairs() + { + var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqQueryExpression(CreateDictionary()); + + Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value)); + } + + [Fact] + public void GivenDuplicateValues_WhenSortedByValueThenKey_ThenTiesAreOrderedByKey() { var dictionary = new Dictionary { + { "2", "apple" }, { "1", "apple" }, - { "2", "orange" }, - { "3", "banana" }, - { "4", "grape" } + { "3", "banana" } }; - var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingSortMethod(dictionary); + var sortedKeyValuePairs = SortDictionary.SortDictionaryValueThenKey(dictionary); - Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple"); - Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana"); - Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape"); - Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange"); + Assert.Equal(["1", "2", "3"], sortedKeyValuePairs.Select(x => x.Key)); } [Fact] - public void GivenUnorderedDictionary_WhenSortedUsingLinqQueryExpression_ThenReturnOrderedKeyValuePairs() + public void GivenUnorderedKeys_WhenLoadedIntoSortedDictionary_ThenEnumeratesByKey() { - var dictionary = new Dictionary + var stock = new Dictionary { - { "1", "apple" }, - { "2", "orange" }, - { "3", "banana" }, - { "4", "grape" } + { "orange", 12 }, + { "apple", 7 }, + { "banana", 30 } }; - var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionary); + var sortedStock = SortDictionary.SortDictionaryKeyUsingSortedDictionary(stock); + + Assert.Equal(["apple", "banana", "orange"], sortedStock.Keys); + } + + [Fact] + public void GivenUnorderedDictionary_WhenSortedIntoOrderedDictionary_ThenKeepsValueOrder() + { + var orderedByValue = SortDictionary.KeepValueOrderInOrderedDictionary(CreateDictionary()); - Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple"); - Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana"); - Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape"); - Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange"); + Assert.Equal(ExpectedValues, orderedByValue.Values); + Assert.Equal("banana", orderedByValue.GetAt(1).Value); } -} \ No newline at end of file +}