Skip to content

Commit 17a0373

Browse files
committed
fix: actually handle undefined sorting out of the box
1 parent 76a87cc commit 17a0373

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

packages/table-core/src/features/Sorting.ts

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const Sorting: TableFeature = {
116116
getDefaultColumnDef: <TData extends RowData>(): SortingColumnDef<TData> => {
117117
return {
118118
sortingFn: 'auto',
119+
sortUndefined: 1,
119120
}
120121
},
121122

packages/table-core/src/utils/getSortedRowModel.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,31 @@ export function getSortedRowModel<TData extends RowData>(): (
5353
const columnInfo = columnInfoById[sortEntry.id]!
5454
const isDesc = sortEntry?.desc ?? false
5555

56+
let sortInt = 0
57+
58+
// All sorting ints should always return in ascending order
5659
if (columnInfo.sortUndefined) {
5760
const aValue = rowA.getValue(sortEntry.id)
5861
const bValue = rowB.getValue(sortEntry.id)
5962

60-
const aUndefined = typeof aValue === 'undefined'
61-
const bUndefined = typeof bValue === 'undefined'
63+
const aUndefined = aValue === undefined
64+
const bUndefined = bValue === undefined
6265

6366
if (aUndefined || bUndefined) {
64-
let undefinedSort =
67+
sortInt =
6568
aUndefined && bUndefined
6669
? 0
6770
: aUndefined
6871
? columnInfo.sortUndefined
6972
: -columnInfo.sortUndefined
70-
71-
if (isDesc && undefinedSort !== 0) {
72-
undefinedSort *= -1
73-
}
74-
75-
return undefinedSort
7673
}
7774
}
7875

79-
// This function should always return in ascending order
80-
let sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id)
76+
if (sortInt === 0) {
77+
sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id)
78+
}
8179

80+
// If sorting is non-zero, take care of desc and inversion
8281
if (sortInt !== 0) {
8382
if (isDesc) {
8483
sortInt *= -1

0 commit comments

Comments
 (0)