From c7c595e416daf364d38366000cfff0309f1d5338 Mon Sep 17 00:00:00 2001 From: Yeongbin Jeong Date: Mon, 23 Nov 2026 00:01:22 +0900 Subject: [PATCH] fix(table-core): sort long digit runs by value in alphanumeric sort Digit runs longer than 15 significant digits fell back to parseInt, which rounds once the value no longer fits in a double, so runs such as 1100000000000000001 and 1100000000000000002 compared as equal. Compare the significant lengths instead, and runs of equal length as digit strings. --- .changeset/long-digit-alphanumeric-sort.md | 5 +++++ .../src/features/row-sorting/sortFns.ts | 17 ++++++++++++----- .../table-core/tests/unit/fns/sortFns.test.ts | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .changeset/long-digit-alphanumeric-sort.md diff --git a/.changeset/long-digit-alphanumeric-sort.md b/.changeset/long-digit-alphanumeric-sort.md new file mode 100644 index 0000000000..0a22fb2845 --- /dev/null +++ b/.changeset/long-digit-alphanumeric-sort.md @@ -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. diff --git a/packages/table-core/src/features/row-sorting/sortFns.ts b/packages/table-core/src/features/row-sorting/sortFns.ts index 5d9a356e00..a83b2d2a55 100644 --- a/packages/table-core/src/features/row-sorting/sortFns.ts +++ b/packages/table-core/src/features/row-sorting/sortFns.ts @@ -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 diff --git a/packages/table-core/tests/unit/fns/sortFns.test.ts b/packages/table-core/tests/unit/fns/sortFns.test.ts index c33f0fdca3..b889433564 100644 --- a/packages/table-core/tests/unit/fns/sortFns.test.ts +++ b/packages/table-core/tests/unit/fns/sortFns.test.ts @@ -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'