Does anyone know how to write a custom sorting fn using Tanstack's React-table? I've been trying to sort a column based on priority (high, medium, low) but the docs are a little confusing as to how to implement.
So far I've declared the module like the docs say:
declare module "@tanstack/table-core" {
interface SortingFns {
myCustomSorting: SortingFn<unknown>;
}
}
Then I used the sortingFns in my table definition with a simple function just to see if I could get it working:
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
state: {
sorting,
},
sortingFns: {
myCustomSorting: (rowA: any, rowB: any, columnId: any): number => {
console.log("hi");
return 0;
},
},
});
I'm calling the toggleSorting fn on my column per click:
<TableHead key={header.id} className="text-center">
{header.isPlaceholder ? null : (
<div
onClick={() => header.column.toggleSorting()}
>
{flexRender(
header.column.columnDef.header,
header.getContext()
)}
</div>
)}
</TableHead>
And finally, I defined the sortingFn in my column definition:
{
id: "Priority",
accessorKey: "priority",
header: "Priority",
sortingFn: "myCustomSorting"
},
I am seeing a console log on click, I guess I'm unsure how to implement the sorting function