Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isDesc to sortingFn #5411

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions docs/api/features/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Returns whether this column is sorted.

### `getFirstSortDir`

```tsx
```tsx
getFirstSortDir: () => SortDirection
```

Expand Down Expand Up @@ -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,
},
})
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
})
Expand Down Expand Up @@ -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' -> ...`

Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/features/Sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface SortingTableState {
}

export interface SortingFn<TData extends RowData> {
(rowA: Row<TData>, rowB: Row<TData>, columnId: string): number
(rowA: Row<TData>, rowB: Row<TData>, columnId: string, isDesc?: boolean): number
}

export type CustomSortingFns<TData extends RowData> = Record<
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/utils/getSortedRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getSortedRowModel<TData extends RowData>(): (
}

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
Expand Down
Loading