Skip to content
This repository was archived by the owner on Jul 31, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/components/Table/sorting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,41 @@ describe("Table sorting helpers", () => {
it("compares strings, numbers, booleans, dates, and nulls consistently", () => {
expect(compareSortValues("a", "b")).toBeLessThan(0);
expect(compareSortValues(2, 1)).toBeGreaterThan(0);
expect(compareSortValues(false, true)).toBeLessThan(0);
expect(compareSortValues(true, false)).toBeLessThan(0);
expect(compareSortValues(false, true)).toBeGreaterThan(0);
expect(
compareSortValues(new Date("2026-05-01"), new Date("2026-05-02")),
).toBeLessThan(0);
expect(compareSortValues(null, "a")).toBeGreaterThan(0);
expect(compareSortValues(undefined, null)).toBe(0);
});

it("sortRows puts enrolled employees first in ascending boolean order", () => {
type Employee = { name: string; enrolled: boolean };

const employees: Employee[] = [
{ name: "Not enrolled", enrolled: false },
{ name: "Enrolled", enrolled: true },
];
const enrolledColumn: Column<Employee> = {
key: "enrolled",
header: "Role",
sortable: true,
sortValue: (employee) => employee.enrolled,
};

expect(
sortRows(employees, enrolledColumn, "asc").map(
(employee) => employee.name,
),
).toEqual(["Enrolled", "Not enrolled"]);
expect(
sortRows(employees, enrolledColumn, "desc").map(
(employee) => employee.name,
),
).toEqual(["Not enrolled", "Enrolled"]);
});

it("sortRows keeps null values last in both directions", () => {
const dateColumn = columns[1];

Expand Down
5 changes: 5 additions & 0 deletions src/components/Table/sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export function compareSortValues(a: SortValue, b: SortValue): number {
const normalizedA = normalizeSortValue(a);
const normalizedB = normalizeSortValue(b);

if (typeof normalizedA === "boolean" && typeof normalizedB === "boolean") {
if (normalizedA === normalizedB) return 0;
return normalizedA ? -1 : 1;
}

if (typeof normalizedA === "string" && typeof normalizedB === "string") {
return normalizedA.localeCompare(normalizedB, undefined, {
numeric: true,
Expand Down