Skip to content
This repository was archived by the owner on Jul 31, 2026. It is now read-only.

Commit c7722d0

Browse files
cursoragentsvadrutk
andcommitted
fix(table): sort true boolean values first
Co-authored-by: Swad K. <svadrutk@users.noreply.github.com>
1 parent 4fcbeac commit c7722d0

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/components/Table/sorting.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,41 @@ describe("Table sorting helpers", () => {
7070
it("compares strings, numbers, booleans, dates, and nulls consistently", () => {
7171
expect(compareSortValues("a", "b")).toBeLessThan(0);
7272
expect(compareSortValues(2, 1)).toBeGreaterThan(0);
73-
expect(compareSortValues(false, true)).toBeLessThan(0);
73+
expect(compareSortValues(true, false)).toBeLessThan(0);
74+
expect(compareSortValues(false, true)).toBeGreaterThan(0);
7475
expect(
7576
compareSortValues(new Date("2026-05-01"), new Date("2026-05-02")),
7677
).toBeLessThan(0);
7778
expect(compareSortValues(null, "a")).toBeGreaterThan(0);
7879
expect(compareSortValues(undefined, null)).toBe(0);
7980
});
8081

82+
it("sortRows puts enrolled employees first in ascending boolean order", () => {
83+
type Employee = { name: string; enrolled: boolean };
84+
85+
const employees: Employee[] = [
86+
{ name: "Not enrolled", enrolled: false },
87+
{ name: "Enrolled", enrolled: true },
88+
];
89+
const enrolledColumn: Column<Employee> = {
90+
key: "enrolled",
91+
header: "Role",
92+
sortable: true,
93+
sortValue: (employee) => employee.enrolled,
94+
};
95+
96+
expect(
97+
sortRows(employees, enrolledColumn, "asc").map(
98+
(employee) => employee.name,
99+
),
100+
).toEqual(["Enrolled", "Not enrolled"]);
101+
expect(
102+
sortRows(employees, enrolledColumn, "desc").map(
103+
(employee) => employee.name,
104+
),
105+
).toEqual(["Not enrolled", "Enrolled"]);
106+
});
107+
81108
it("sortRows keeps null values last in both directions", () => {
82109
const dateColumn = columns[1];
83110

src/components/Table/sorting.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export function compareSortValues(a: SortValue, b: SortValue): number {
3636
const normalizedA = normalizeSortValue(a);
3737
const normalizedB = normalizeSortValue(b);
3838

39+
if (typeof normalizedA === "boolean" && typeof normalizedB === "boolean") {
40+
if (normalizedA === normalizedB) return 0;
41+
return normalizedA ? -1 : 1;
42+
}
43+
3944
if (typeof normalizedA === "string" && typeof normalizedB === "string") {
4045
return normalizedA.localeCompare(normalizedB, undefined, {
4146
numeric: true,

0 commit comments

Comments
 (0)