Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/long-digit-alphanumeric-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/table-core': patch
---

Sort digit runs longer than 15 digits, such as snowflake IDs, by value in `sortFn_alphanumeric` and `sortFn_alphanumericCaseSensitive`. They previously compared as equal once `parseInt` lost precision.
17 changes: 12 additions & 5 deletions packages/table-core/src/features/row-sorting/sortFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,21 @@ function compareNumericChunks(
return 0
}

// Preserve parseInt precision-collapse behavior for unusually large chunks.
const an = parseInt(aStr.slice(aStart, aEnd), 10)
const bn = parseInt(bStr.slice(bStart, bEnd), 10)
// Too long to parse exactly: compare by length, then digit by digit.
if (aSignificantLength > bSignificantLength) {
return 1
}
if (bSignificantLength > aSignificantLength) {
return -1
}

const aDigits = aStr.slice(aSignificantStart, aEnd)
const bDigits = bStr.slice(bSignificantStart, bEnd)

if (an > bn) {
if (aDigits > bDigits) {
return 1
}
if (bn > an) {
if (bDigits > aDigits) {
return -1
}
return 0
Expand Down
15 changes: 15 additions & 0 deletions packages/table-core/tests/unit/fns/sortFns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe('sortFn_alphanumeric (compareAlphanumeric)', () => {
expect(cmp(sortFn_alphanumeric, Infinity, '')).toBe(0)
})

it('sorts digit runs beyond number precision numerically', () => {
expect(
cmp(sortFn_alphanumeric, '1100000000000000001', '1100000000000000002'),
).toBe(-1)
expect(
cmp(sortFn_alphanumeric, '1100000000000000002', '1100000000000000001'),
).toBe(1)
expect(
cmp(sortFn_alphanumeric, '9999999999999999', '10000000000000000'),
).toBe(-1)
expect(
cmp(sortFn_alphanumeric, '001100000000000000001', '1100000000000000001'),
).toBe(0)
})

it('handles long mixed strings (stress: many chunks)', () => {
const a = 'abc1def2ghi3jkl4mno5'
const b = 'abc1def2ghi3jkl4mno10'
Expand Down