diff --git a/docs/api/features/sorting.md b/docs/api/features/sorting.md index 1e755889ad..1c43ad648c 100644 --- a/docs/api/features/sorting.md +++ b/docs/api/features/sorting.md @@ -201,7 +201,7 @@ Returns whether this column is sorted. ### `getFirstSortDir` -```tsx +```tsx getFirstSortDir: () => SortDirection ``` @@ -256,7 +256,7 @@ const column = columnHelper.data('key', { const table = useReactTable({ columns: [column], sortingFns: { - myCustomSorting: (rowA: any, rowB: any, columnId: any): number => + myCustomSorting: (rowA: any, rowB: any, columnId: any, isDesc: boolean): number => rowA.getValue(columnId).value < rowB.getValue(columnId).value ? 1 : -1, }, }) diff --git a/docs/guide/sorting.md b/docs/guide/sorting.md index 1c17524f3c..d99f8a8d68 100644 --- a/docs/guide/sorting.md +++ b/docs/guide/sorting.md @@ -199,8 +199,8 @@ const table = useReactTable({ getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), sortingFns: { //add a custom sorting function - myCustomSortingFn: (rowA, rowB, columnId) => { - return rowA.original[columnId] > rowB.original[columnId] ? 1 : rowA.original[columnId] < rowB.original[columnId] ? -1 : 0 + myCustomSortingFn: (rowA, rowB, columnId, isDesc) => { + return rowA.original[columnId] > rowB.original[columnId] ? 1 : rowA.original[columnId] < rowB.original[columnId] ? -1 : 0 }, }, }) @@ -302,7 +302,7 @@ const columns = [ By default, the ability to remove sorting while cycling through the sorting states for a column is enabled. You can disable this behavior using the `enableSortingRemoval` table option. This behavior is useful if you want to ensure that at least one column is always sorted. -The default behavior when using either the `getToggleSortingHandler` or `toggleSorting` APIs is to cycle through the sorting states like this: +The default behavior when using either the `getToggleSortingHandler` or `toggleSorting` APIs is to cycle through the sorting states like this: `'none' -> 'desc' -> 'asc' -> 'none' -> 'desc' -> 'asc' -> ...` diff --git a/packages/table-core/src/features/Sorting.ts b/packages/table-core/src/features/Sorting.ts index 930c328487..3f207724ed 100644 --- a/packages/table-core/src/features/Sorting.ts +++ b/packages/table-core/src/features/Sorting.ts @@ -32,7 +32,7 @@ export interface SortingTableState { } export interface SortingFn { - (rowA: Row, rowB: Row, columnId: string): number + (rowA: Row, rowB: Row, columnId: string, isDesc?: boolean): number } export type CustomSortingFns = Record< diff --git a/packages/table-core/src/utils/getSortedRowModel.ts b/packages/table-core/src/utils/getSortedRowModel.ts index 7c95bbd699..36e8504ff7 100644 --- a/packages/table-core/src/utils/getSortedRowModel.ts +++ b/packages/table-core/src/utils/getSortedRowModel.ts @@ -74,7 +74,7 @@ export function getSortedRowModel(): ( } if (sortInt === 0) { - sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id) + sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id, isDesc) } // If sorting is non-zero, take care of desc and inversion