Digitemal.DynamicComparer
0.1.0-alpha.1
dotnet add package Digitemal.DynamicComparer --version 0.1.0-alpha.1
NuGet\Install-Package Digitemal.DynamicComparer -Version 0.1.0-alpha.1
<PackageReference Include="Digitemal.DynamicComparer" Version="0.1.0-alpha.1" />
<PackageVersion Include="Digitemal.DynamicComparer" Version="0.1.0-alpha.1" />
<PackageReference Include="Digitemal.DynamicComparer" />
paket add Digitemal.DynamicComparer --version 0.1.0-alpha.1
#r "nuget: Digitemal.DynamicComparer, 0.1.0-alpha.1"
#:package Digitemal.DynamicComparer@0.1.0-alpha.1
#addin nuget:?package=Digitemal.DynamicComparer&version=0.1.0-alpha.1&prerelease
#tool nuget:?package=Digitemal.DynamicComparer&version=0.1.0-alpha.1&prerelease
DynamicComparer
A lightweight, composable .NET library for strongly typed and dynamic property-based ordering, without custom comparer classes. It is an independent community project and is not endorsed by Microsoft or the .NET Foundation. The proposal that inspired this project is dotnet/runtime issue 138583.
Targets: netstandard2.0, net8.0, and net10.0.
Install
dotnet add package Digitemal.DynamicComparer
dotnet add package Digitemal.DynamicComparer --prerelease
Typed ordering
var comparer = DynamicComparer<Employee>.By(x => x.Department)
.ThenByDescending(x => x.Salary)
.ThenBy(x => x.Id);
employees.Sort(comparer);
Property paths and null placement
var comparer = DynamicComparer<Employee>.By("Department.Company.Name", NullOrdering.Last)
.ThenByDescending("Salary");
employees.Sort(comparer);
Dynamic paths resolve public readable instance properties eagerly and are case-sensitive. Validate user-controlled names against an application allowlist before creating the comparer. Getters can execute consumer code.
Custom comparers and composition
var names = DynamicComparer<Employee>.By(x => x.Name, StringComparer.OrdinalIgnoreCase);
IComparer<Employee> combined = names.Then(DynamicComparer<Employee>.ByDescending(x => x.Salary));
var standard = Comparer<Employee>.Create((x, y) => x.Age.CompareTo(y.Age)).ThenBy(x => x.Name);
Runtime rules
var rules = new[] {
new OrderingRule("Department.Name", SortDirection.Ascending, NullOrdering.Last),
new OrderingRule("Salary", SortDirection.Descending)
};
var comparer = DynamicComparer<Employee>.From(rules);
An empty rule list, invalid direction, null rule, or invalid path is rejected before the comparer is returned. Repeated paths are permitted.
Collections and semantics
Any IComparer<T> works with List<T>.Sort, Array.Sort, SortedSet<T>, SortedDictionary<TKey,TValue>, and other comparer-taking APIs. Append a unique ID when distinct entities must not compare equal. A zero result means ordering equivalence, so sorted sets and dictionaries may collapse distinct values. List.Sort is unstable; LINQ OrderBy uses key sorting and has different execution and stability behavior. IComparer<T> is not IEqualityComparer<T> and cannot generally be translated to SQL by IQueryable providers. Enumerable.OrderBy accepts a key selector (identity can be used for values); it does not take an arbitrary comparer for the sequence itself.
AOT, trimming, and performance
Strongly typed selector rules are intended for Native AOT use. String paths depend on reflection metadata and runtime expression compilation; no unrestricted trimming/AOT guarantee is made for them. Benchmarks use BenchmarkDotNet; consult performance notes for methodology. No performance superiority claim is made without committed measurements.
Thread safety follows immutable comparer state when supplied selectors/comparers are themselves thread-safe. See API specification, contributing, and release procedure.
GitHub Packages
The feed is https://nuget.pkg.github.com/Digitemal/index.json. Consumers need an authenticated NuGet source and appropriate package access; do not place tokens in source files. See setup.
For ASP.NET Core request parameters, map a small allowlist to approved paths before creating rules; never pass arbitrary request text directly to the resolver:
var allowed = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["name"] = "Name",
["salary"] = "Salary",
["department"] = "Department.Name"
};
if (!allowed.TryGetValue(request.SortBy ?? "", out var path))
return Results.BadRequest();
var comparer = DynamicComparer<Employee>.By(path);
This library has no ASP.NET Core dependency. IComparer<T> orders objects; OrderBy orders LINQ sequences; IQueryable providers may not translate this comparer; equality comparison uses IEqualityComparer<T> instead.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-alpha.1 | 43 | 12/2/2026 |