compareProperties | WebHare Platform SDK
WebHare Platform SDK
    Preparing search index...

    Function compareProperties

    • Build a comparator function for objects using one or more comparable properties. This is extremely useful as a type-safe comparator for sort/toSorted APIs, especially when dealing with multiple or non-string keys.

      Keys can be provided as:

      • a single property key
      • a tuple/array of keys
      • a tuple/array with sort direction entries in the form [key, "asc" | "desc"]

      Comparison is performed in order until the first non-zero result.

      Type Parameters

      • const K extends string | number | symbol | ComparePropertiesArrayElement<E>[]
      • E extends object = Record<GetPropertiesFromComparePropertiesKeys<K>, ComparableType>

      Parameters

      • fields: K

        Field selector(s) defining compare priority and optional direction.

      Returns (lhs: E, rhs: E) => number & {
          partialCompare: <
              T extends
                  | (
                      ([unknown] | [ComparePropertiesArrayElement<E>]) & (string | number | symbol)
                  )
                  | (
                      ([unknown] | [ComparePropertiesArrayElement<E>]) & [ComparePropertiesArrayElement<E>, ...ComparePropertiesArrayElement<E>[]]
                  ),
          >(
              partialFields: T,
          ) => (lhs: E, rhs: E) => number & {
              partialCompare: <
                  T extends
                      | (
                          ([unknown] | [ComparePropertiesArrayElement<E>] | [string | ComparePropertiesArrayElement<E>]) & (string | number | symbol)
                      )
                      | (
                          ([unknown] | [ComparePropertiesArrayElement<E>] | [string | ComparePropertiesArrayElement<E>]) & [ComparePropertiesArrayElement<E>, ...ComparePropertiesArrayElement<...>[]]
                      ),
              >(
                  partialFields: T,
              ) => (lhs: E, rhs: E) => number & {
                  partialCompare: <
                      T extends
                          | (
                              ([unknown] | [ComparePropertiesArrayElement<E>] | [string | ComparePropertiesArrayElement<E>] | [(string | ComparePropertiesArrayElement<...>) & ComparePropertiesArrayElement<...>]) & (string | ... 1 more ... | symbol)
                          )
                          | (
                              ([unknown] | [ComparePropertiesArrayElement<E>] | [string | ComparePropertiesArrayElement<E>] | [(string | ComparePropertiesArrayElement<...>) & ComparePropertiesArrayElement<...>]) & [...]
                          ),
                  >(
                      partialFields: T,
                  ) => (lhs: ..., rhs: ...) => ... & { partialCompare: ... };
              };
          };
      }

      A compare callback suitable for Array.prototype.sort, extended with partialCompare(...) to create a comparator for a prefix of the same key sequence.

      Error if any configured property is undefined on either compared object.

      const byName = compareProperties("name");
      users.sort(byName);
      const byPriorityThenDate = compareProperties([
      ["priority", "desc"],
      ["createdAt", "asc"]
      ] as const);
      items.sort(byPriorityThenDate);
      const byCountryCityStreet = compareProperties(["country", "city", "street"] as const);
      const byCountryCity = byCountryCityStreet.partialCompare(["country", "city"] as const);
      addresses.sort(byCountryCity);