Skip to content

SortListByProperty: .NET 10, fixed benchmark, descending sort, honest tests - #2267

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/68261-sort-list-by-object-property-dotnet
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/68261-sort-list-by-object-property-dotnet

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates collections-lists/SortListByProperty for the refreshed article "Sort a List of Objects by Property in C#" (https://code-maze.com/sort-list-by-object-property-dotnet/). Only this folder changes.

Packages (re-queried on NuGet 2026-10-04, newest stable):

  • net6.0 to net10.0 in both projects
  • BenchmarkDotNet 0.13.1 to 0.15.8
  • MSTest.TestAdapter + MSTest.TestFramework 2.2.7 to the single MSTest 4.4.1 meta package
  • Microsoft.NET.Test.Sdk 16.11.0 to 18.10.1
  • coverlet.collector 3.1.0 to 10.1.0
  • dotnet list package --vulnerable --include-transitive: clean

Benchmark fix:

  • Three of the four benchmarks sorted the one shared ArgumentsSource list in place, so every invocation after the first sorted an already sorted list. Each invocation now gets a fresh unsorted copy through IterationSetup.
  • SortByTitleIComparable sorted by Pages (Book.CompareTo compares Pages), not by Title. Renamed SortByPagesIComparable, with a like-for-like SortByPagesUsingLinq row, plus SortByPagesDescendingUsingLinq. Benchmarks live in their own SortBenchmarks class.

Tests:

  • The multiple-properties test could not fail: the unsorted input was already in the expected order. The input order changes, and a new test checks the result differs from the input and from an Author-only sort. Both tests fail against a method that returns its input or sorts by Author only.
  • Descending sample and a test for each form (OrderByDescending and the in-place operand swap).
  • 9 tests, all passing on .NET 10.0.10. Build: 0 warnings, 0 errors (the nine nullable warnings are fixed).

Idioms: file-scoped namespaces, Random.Shared, target-typed new(), required properties, nullable comparer parameters.

Built and benchmarked on Windows 10 (10.0.19045), SDK 10.0.302, runtime .NET 10.0.10, BenchmarkDotNet 0.15.8.

… tests

- Retarget both projects to net10.0; BenchmarkDotNet 0.15.8, MSTest 4.4.1
  (one meta package replaces the adapter and framework), Microsoft.NET.Test.Sdk
  18.10.1, coverlet.collector 10.1.0.
- Benchmark: every invocation now sorts a fresh unsorted copy (IterationSetup),
  so the in-place rows no longer sort an already sorted list. The IComparable
  row sorts by Pages, so it is renamed SortByPagesIComparable and gets a
  like-for-like SortByPagesUsingLinq row, plus SortByPagesDescendingUsingLinq.
  Benchmarks move to their own SortBenchmarks class.
- The multiple-properties test could not fail: the unsorted input was already
  in the expected order. The input order changes and a second test proves the
  result differs from the input and from an Author-only sort.
- Descending sample: SortByPagesDescendingUsingLinq and the in-place
  Sort((x, y) => y.Pages.CompareTo(x.Pages)) form, with a test for each.
- File-scoped namespaces, Random.Shared, target-typed new(), required
  properties and nullable parameters on the comparers (nine nullable warnings
  gone), unused benchmark variable dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant