export const indexOfCompareFn =
  <T, K>(arr: T[], keyBy: (t: T) => K) =>
  <U extends T>(a: U, b: U) => {
    const aKey = keyBy(a);
    const bKey = keyBy(b);

    const aIndex = arr.findIndex((e) => keyBy(e) === aKey);
    const bIndex = arr.findIndex((e) => keyBy(e) === bKey);

    if (aIndex === -1 && bIndex === -1) {
      return 0;
    }

    // if A is missing in arr, place A after B
    if (aIndex === -1) {
      return 1;
    }

    // if b is missing in arr, place A before B
    if (bIndex === -1) {
      return -1;
    }

    return aIndex - bIndex;
  };
