Path: redb/redb.Core/Utils/RedbDictionaryOrder.cs Snapshot: redb, 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 Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Utils/RedbDictionaryOrder.cs ---- using System; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace redb.Core.Utils; /// /// The order in which a dictionary read from the database lists its entries: by key, the same on every provider and /// in every read. A dictionary is stored as an unordered set (its hash ignores the order of entries), and a read used /// to list them in the order of its rows (Pro) or of the JSON the free modules built (by key length on PostgreSQL, by /// key text on SQL Server and SQLite), so the order moved between providers and, in Pro, between reads. /// Simple keys sort by their value: strings ordinally, numbers, dates, times, Guids and enums as values, byte arrays /// by their bytes. Composite keys (value tuples, classes) sort by the text they are stored under. /// public static class RedbDictionaryOrder { private static readonly ConditionalWeakTable> ByKeyType = new(); /// The comparer of keys of type . public static IComparer For(Type keyType) => ByKeyType.GetValue(keyType, Create); /// The entries in the order a read lists them. public static List> Sort(List> entries) where TKey : notnull { var comparer = For(typeof(TKey)); entries.Sort((a, b) => comparer.Compare(a.Key, b.Key)); return entries; } private static IComparer Create(Type keyType) { if (keyType == typeof(string)) return Comparer.Create(static (a, b) => string.CompareOrdinal((string)a, (string)b)); if (keyType == typeof(byte[])) return Comparer.Create(static (a, b) => ((byte[])a).AsSpan().SequenceCompareTo((byte[])b)); if (!RedbKeySerializer.IsComplexKey(keyType) && typeof(IComparable).IsAssignableFrom(keyType)) return Comparer.Create(static (a, b) => ((IComparable)a).CompareTo(b)); return Comparer.Create((a, b) => string.CompareOrdinal( RedbKeySerializer.SerializeObject(a, keyType), RedbKeySerializer.SerializeObject(b, keyType))); } }