Function sortObject

  • Category

    Sort an object's keys by comparing their respective values.

    Time complexity: O(n)

    Space complexity: O(1)

    Type Parameters

    • K extends PropertyKey
    • V

    Parameters

    • object: Readonly<Record<K, V>>

      Object to sort

    • compareFn: CompareFn<V>

      Function used to compare object values

    Returns [K, V][]

    A new object with the keys in order

    Sort

    import { Sort } from '@jonahsnider/util';

    const object = { a: 3, c: 1, b: 2 };

    Object.fromEntries(sortObject(object, Sort.ascending));