Path: redb-route/redb.Route/Definitions/SortDefinition.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/SortDefinition.cs ---- using System.Collections; using System.Globalization; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Processors; namespace redb.Route.Definitions; /// /// Sorts a collection taken from the exchange and puts the sorted list back into the body /// (Camel sort()). The optional key selector is evaluated per element; in the string form /// (Sort("body.items", "body.priority")) the key expression sees the element as body. /// Numbers compare numerically across CLR types, strings ordinally, null first. /// public sealed class SortDefinition : ProcessorDefinition { private readonly Func _source; private readonly Func? _key; private readonly IComparer _comparer; private readonly bool _descending; private readonly Func, object> _materialize; /// Creates the node. /// Collection to sort (evaluated on the exchange). /// Optional per-element key; null sorts the elements themselves. /// Optional comparer of keys; default compares numbers numerically, strings ordinally. /// Reverse order. /// Builds the body from the sorted elements (a typed List<T> for the generic DSL form). public SortDefinition(Func source, Func? key, IComparer? comparer, bool descending, Func, object>? materialize = null) { _source = source ?? throw new ArgumentNullException(nameof(source)); _key = key; _comparer = comparer ?? ValueComparer.Instance; _descending = descending; _materialize = materialize ?? (items => items.ToList()); } /// public override IProcessor CreateProcessor(IRouteContext context) => new DelegateProcessor(exchange => { var source = _source(exchange); if (source is null) return; if (source is string) throw new InvalidOperationException("Sort: the expression evaluated to a string, not a collection."); var items = source.Cast(); var keyed = _key is null ? items.Select(i => (Key: i, Item: i)) : items.Select(i => (Key: _key(i), Item: i)); var ordered = _descending ? keyed.OrderByDescending(p => p.Key, _comparer) : keyed.OrderBy(p => p.Key, _comparer); exchange.In.Body = _materialize(ordered.Select(p => p.Item)); }); /// Default key comparison: nulls first, numbers numerically, strings ordinally, then . public sealed class ValueComparer : IComparer { /// Shared instance. public static readonly ValueComparer Instance = new(); /// public int Compare(object? x, object? y) { if (ReferenceEquals(x, y)) return 0; if (x is null) return -1; if (y is null) return 1; if (IsNumber(x) && IsNumber(y)) return RouteDecimal.From(x).CompareTo(RouteDecimal.From(y)); if (x is string sx && y is string sy) return string.CompareOrdinal(sx, sy); if (x is IComparable cx && x.GetType() == y.GetType()) return cx.CompareTo(y); return string.CompareOrdinal(Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); } private static bool IsNumber(object value) => value is sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal; } }