ChemPal Documentation - v1.6.0
    Preparing search index...

    Function unitPriceSortingFn

    • TanStack sorting comparator that orders two product rows by their price per base unit (getUnitPrice). Rows whose unit price can't be computed (missing price/quantity or an unconvertible unit) sort to the bottom rather than appearing as the cheapest.

      Parameters

      • rowA: Row<Product>

        The first row to compare.

      • rowB: Row<Product>

        The second row to compare.

      Returns -1 | 0 | 1

      1 if rowA ranks after rowB, -1 if before, 0 if equal.

      useReactTable({ sortingFns: { unitPriceSortingFn }, ... });
      // A row at $0.10/g sorts after one at $0.02/g.
      export function unitPriceSortingFn(rowA: Row<Product>, rowB: Row<Product>) {
      const a = getUnitPrice(rowA.original) ?? Number.POSITIVE_INFINITY;
      const b = getUnitPrice(rowB.original) ?? Number.POSITIVE_INFINITY;
      return a > b ? 1 : a < b ? -1 : 0;
      }