Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public IEnumerable<object> DictionaryInput()

[Benchmark]
[ArgumentsSource(nameof(DictionaryInput))]
public void SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput)
public List<KeyValuePair<string, string>> SortDictionaryValueUsingLinqOrderBy(DictionaryInput dictionaryInput)
{
SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary);
return SortDictionary.SortDictionaryValueUsingLinqOrderBy(dictionaryInput.GeneratedDictionary);
}

[Benchmark]
[ArgumentsSource(nameof(DictionaryInput))]
public void SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput)
public List<KeyValuePair<string, string>> SortDictionaryValueUsingLinqQueryExpression(DictionaryInput dictionaryInput)
{
SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary);
return SortDictionary.SortDictionaryValueUsingLinqQueryExpression(dictionaryInput.GeneratedDictionary);
}

[Benchmark]
[ArgumentsSource(nameof(DictionaryInput))]
public void SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput)
public List<KeyValuePair<string, string>> SortDictionaryValueUsingSortMethod(DictionaryInput dictionaryInput)
{
SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary);
return SortDictionary.SortDictionaryValueUsingSortMethod(dictionaryInput.GeneratedDictionary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,28 @@ public static List<KeyValuePair<string, string>> SortDictionaryValueUsingSortMet

return sortedKeyValuePairs;
}
}

public static List<KeyValuePair<string, string>> SortDictionaryValueThenKey(Dictionary<string, string> dictionary)
{
var sortedKeyValuePairs = dictionary
.OrderBy(x => x.Value)
.ThenBy(x => x.Key)
.ToList();

return sortedKeyValuePairs;
}

public static SortedDictionary<string, int> SortDictionaryKeyUsingSortedDictionary(Dictionary<string, int> stock)
{
var sortedStock = new SortedDictionary<string, int>(stock);

return sortedStock;
}

public static OrderedDictionary<string, string> KeepValueOrderInOrderedDictionary(Dictionary<string, string> dictionary)
{
var orderedByValue = new OrderedDictionary<string, string>(dictionary.OrderBy(x => x.Value));

return orderedByValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,76 @@ namespace SortDictionaryByValueTests;

public class SortDictionaryByValueUnitTest
{
private static Dictionary<string, string> 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<string, string>
{
{ "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<string, string>
{
{ "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<string, string>
var stock = new Dictionary<string, int>
{
{ "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);
}
}
}
Loading