Skip to content

Commit 4438423

Browse files
Sort Dictionary by Value: move to .NET 10, fix test asserts, add printed samples (#2340)
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.
1 parent 53d5125 commit 4438423

6 files changed

Lines changed: 88 additions & 48 deletions

File tree

‎collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValue.Benchmark.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

‎collections-dictionary/SortDictionaryByValue/SortDictionaryByValue.Benchmark/SortDictionaryByValueBenchMark.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ public IEnumerable<object> DictionaryInput()
1212

1313
[Benchmark]
1414
[ArgumentsSource(nameof(DictionaryInput))]
15-
public void SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput)
15+
public List<KeyValuePair<string, string>> SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput)
1616
{
17-
SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary);
17+
return SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary);
1818
}
1919

2020
[Benchmark]
2121
[ArgumentsSource(nameof(DictionaryInput))]
22-
public void SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput)
22+
public List<KeyValuePair<string, string>> SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput)
2323
{
24-
SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary);
24+
return SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary);
2525
}
2626

2727
[Benchmark]
2828
[ArgumentsSource(nameof(DictionaryInput))]
29-
public void SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput)
29+
public List<KeyValuePair<string, string>> SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput)
3030
{
31-
SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary);
31+
return SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary);
3232
}
3333
}

‎collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionary.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,28 @@ public static List<KeyValuePair<string, string>> SortDictionaryValueUsingSortMet
2626

2727
return sortedKeyValuePairs;
2828
}
29-
}
29+
30+
public static List<KeyValuePair<string, string>> SortDictionaryValueThenKey(Dictionary<string, string> dictionary)
31+
{
32+
var sortedKeyValuePairs = dictionary
33+
.OrderBy(x => x.Value)
34+
.ThenBy(x => x.Key)
35+
.ToList();
36+
37+
return sortedKeyValuePairs;
38+
}
39+
40+
public static SortedDictionary<string, int> SortDictionaryKeyUsingSortedDictionary(Dictionary<string, int> stock)
41+
{
42+
var sortedStock = new SortedDictionary<string, int>(stock);
43+
44+
return sortedStock;
45+
}
46+
47+
public static OrderedDictionary<string, string> KeepValueOrderInOrderedDictionary(Dictionary<string, string> dictionary)
48+
{
49+
var orderedByValue = new OrderedDictionary<string, string>(dictionary.OrderBy(x => x.Value));
50+
51+
return orderedByValue;
52+
}
53+
}

‎collections-dictionary/SortDictionaryByValue/SortDictionaryByValue/SortDictionaryByValue.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

‎collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueTests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13-
<PackageReference Include="xunit" Version="2.4.1" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
13+
<PackageReference Include="xunit" Version="2.9.3" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="coverlet.collector" Version="3.1.2">
18+
<PackageReference Include="coverlet.collector" Version="10.1.0">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>

‎collections-dictionary/SortDictionaryByValue/Tests/SortDictionaryByValueUnitTest.cs‎

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,76 @@ namespace SortDictionaryByValueTests;
55

66
public class SortDictionaryByValueUnitTest
77
{
8+
private static Dictionary<string, string> CreateDictionary() => new()
9+
{
10+
{ "1", "apple" },
11+
{ "2", "orange" },
12+
{ "3", "banana" },
13+
{ "4", "grape" }
14+
};
15+
16+
private static readonly string[] ExpectedValues = ["apple", "banana", "grape", "orange"];
17+
818
[Fact]
919
public void GivenUnorderedDictionary_WhenSortedUsingLinqOrderBy_ThenReturnOrderedKeyValuePairs()
1020
{
11-
var dictionary = new Dictionary<string, string>
12-
{
13-
{ "1", "apple" },
14-
{ "2", "orange" },
15-
{ "3", "banana" },
16-
{ "4", "grape" }
17-
};
21+
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqOrderBy(CreateDictionary());
1822

19-
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionary);
20-
21-
Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple");
22-
Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana");
23-
Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape");
24-
Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange");
23+
Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value));
2524
}
2625

2726
[Fact]
2827
public void GivenUnorderedDictionary_WhenSortedUsingSortMethod_ThenReturnOrderedKeyValuePairs()
28+
{
29+
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingSortMethod(CreateDictionary());
30+
31+
Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value));
32+
}
33+
34+
[Fact]
35+
public void GivenUnorderedDictionary_WhenSortedUsingLinqQueryExpression_ThenReturnOrderedKeyValuePairs()
36+
{
37+
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqQueryExpression(CreateDictionary());
38+
39+
Assert.Equal(ExpectedValues, sortedKeyValuePairs.Select(x => x.Value));
40+
}
41+
42+
[Fact]
43+
public void GivenDuplicateValues_WhenSortedByValueThenKey_ThenTiesAreOrderedByKey()
2944
{
3045
var dictionary = new Dictionary<string, string>
3146
{
47+
{ "2", "apple" },
3248
{ "1", "apple" },
33-
{ "2", "orange" },
34-
{ "3", "banana" },
35-
{ "4", "grape" }
49+
{ "3", "banana" }
3650
};
3751

38-
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingSortMethod(dictionary);
52+
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueThenKey(dictionary);
3953

40-
Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple");
41-
Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana");
42-
Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape");
43-
Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange");
54+
Assert.Equal(["1", "2", "3"], sortedKeyValuePairs.Select(x => x.Key));
4455
}
4556

4657
[Fact]
47-
public void GivenUnorderedDictionary_WhenSortedUsingLinqQueryExpression_ThenReturnOrderedKeyValuePairs()
58+
public void GivenUnorderedKeys_WhenLoadedIntoSortedDictionary_ThenEnumeratesByKey()
4859
{
49-
var dictionary = new Dictionary<string, string>
60+
var stock = new Dictionary<string, int>
5061
{
51-
{ "1", "apple" },
52-
{ "2", "orange" },
53-
{ "3", "banana" },
54-
{ "4", "grape" }
62+
{ "orange", 12 },
63+
{ "apple", 7 },
64+
{ "banana", 30 }
5565
};
5666

57-
var sortedKeyValuePairs = SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionary);
67+
var sortedStock = SortDictionary.SortDictionaryKeyUsingSortedDictionary(stock);
68+
69+
Assert.Equal(["apple", "banana", "orange"], sortedStock.Keys);
70+
}
71+
72+
[Fact]
73+
public void GivenUnorderedDictionary_WhenSortedIntoOrderedDictionary_ThenKeepsValueOrder()
74+
{
75+
var orderedByValue = SortDictionary.KeepValueOrderInOrderedDictionary(CreateDictionary());
5876

59-
Assert.Same(sortedKeyValuePairs.ElementAt(0).Value, "apple");
60-
Assert.Same(sortedKeyValuePairs.ElementAt(1).Value, "banana");
61-
Assert.Same(sortedKeyValuePairs.ElementAt(2).Value, "grape");
62-
Assert.Same(sortedKeyValuePairs.ElementAt(3).Value, "orange");
77+
Assert.Equal(ExpectedValues, orderedByValue.Values);
78+
Assert.Equal("banana", orderedByValue.GetAt(1).Value);
6379
}
64-
}
80+
}

0 commit comments

Comments
 (0)