Sorts the keys of an object in alphabetical order and returns a new object with the sorted keys.

sortObject({ b: 2, a: 1}) // { a: 1, b: 2 }
  • Type Parameters

    • T extends Record<K, unknown>
    • K extends string | number | symbol

    Parameters

    • obj: T

      The input object to sort.

    • Optionalcomparator: ((a: unknown, b: unknown) => number)

      An optional comparator function to use when sorting the keys.

        • (a, b): number
        • Parameters

          • a: unknown
          • b: unknown

          Returns number

    Returns T

    • A new object with the sorted keys.