Skip to content

Commit 3f9d382

Browse files
authored
fix: better alphanumeric sort of bools and floats (fix #5108) (#5109)
* Fix alphanumeric sort bool support (fix #5108) * Fix alphanumeric sort float support (fix #5108)
1 parent 171648e commit 3f9d382

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
ColumnDef,
3+
createTable,
4+
getCoreRowModel,
5+
getSortedRowModel,
6+
} from '../src'
7+
8+
describe('Sorting', () => {
9+
describe('alphanumeric sort booleans ascending', () => {
10+
it('should return rows in correct ascending order', () => {
11+
const data = [
12+
{ value: false },
13+
{ value: true },
14+
{ value: false },
15+
{ value: true },
16+
]
17+
const columns: ColumnDef<{value: boolean}>[] = [{
18+
accessorKey: "value",
19+
header: "Value",
20+
sortingFn: "alphanumeric"
21+
}]
22+
23+
const table = createTable({
24+
onStateChange() {},
25+
renderFallbackValue: '',
26+
data,
27+
state: { sorting: [{
28+
id: "value",
29+
desc: false,
30+
}]},
31+
columns,
32+
getCoreRowModel: getCoreRowModel(),
33+
getSortedRowModel: getSortedRowModel(),
34+
})
35+
36+
const rowModel = table.getSortedRowModel()
37+
38+
expect(rowModel.rows[0].getValue("value")).toBe(false)
39+
expect(rowModel.rows[1].getValue("value")).toBe(false)
40+
expect(rowModel.rows[2].getValue("value")).toBe(true)
41+
expect(rowModel.rows[3].getValue("value")).toBe(true)
42+
})
43+
})
44+
45+
describe('alphanumeric sort floats ascending', () => {
46+
it('should return rows in correct ascending order', () => {
47+
const data = [
48+
{ value: 0.85 },
49+
{ value: 0.001000000047 },
50+
{ value: 0.2000016 },
51+
{ value: 0.002002 },
52+
]
53+
const columns: ColumnDef<{value: number}>[] = [{
54+
accessorKey: "value",
55+
header: "Value",
56+
sortingFn: "alphanumeric"
57+
}]
58+
59+
const table = createTable({
60+
onStateChange() {},
61+
renderFallbackValue: '',
62+
data,
63+
state: { sorting: [{
64+
id: "value",
65+
desc: false,
66+
}]},
67+
columns,
68+
getCoreRowModel: getCoreRowModel(),
69+
getSortedRowModel: getSortedRowModel(),
70+
})
71+
72+
const rowModel = table.getSortedRowModel()
73+
74+
expect(rowModel.rows[0].getValue("value")).toBe(0.001000000047)
75+
expect(rowModel.rows[1].getValue("value")).toBe(0.002002)
76+
expect(rowModel.rows[2].getValue("value")).toBe(0.2000016)
77+
expect(rowModel.rows[3].getValue("value")).toBe(0.85)
78+
})
79+
})
80+
})

packages/table-core/src/sortingFns.ts

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ function compareBasic(a: any, b: any) {
5555
}
5656

5757
function toString(a: any) {
58+
if (typeof a === 'boolean') {
59+
return String(a)
60+
}
5861
if (typeof a === 'number') {
5962
if (isNaN(a) || a === Infinity || a === -Infinity) {
6063
return ''
@@ -71,6 +74,13 @@ function toString(a: any) {
7174
// It handles numbers, mixed alphanumeric combinations, and even
7275
// null, undefined, and Infinity
7376
function compareAlphanumeric(aStr: string, bStr: string) {
77+
// Check if the string contains only a number
78+
const aFloat = parseFloat(aStr);
79+
const bFloat = parseFloat(bStr);
80+
if(!isNaN(aFloat) && !isNaN(bFloat)) {
81+
return compareBasic(aFloat, bFloat)
82+
}
83+
7484
// Split on number groups, but keep the delimiter
7585
// Then remove falsey split values
7686
const a = aStr.split(reSplitAlphaNumeric).filter(Boolean)

0 commit comments

Comments
 (0)