Returns a new object with keys sorted alphabetically.
Creates a shallow copy of the object with keys in sorted order. Useful for deterministic serialization or comparison.
The input object type
Base type for non-null assertion
Optional
The object to sort
Optional function to transform values during sorting
New object with sorted keys, or undefined if input is null/undefined
const sorted = toSortedObject({ z: 3, a: 1, m: 2 });// { a: 1, m: 2, z: 3 }Object.keys(sorted); // ['a', 'm', 'z'] Copy
const sorted = toSortedObject({ z: 3, a: 1, m: 2 });// { a: 1, m: 2, z: 3 }Object.keys(sorted); // ['a', 'm', 'z']
Returns a new object with keys sorted alphabetically.
Creates a shallow copy of the object with keys in sorted order. Useful for deterministic serialization or comparison.