Skip to content

fix(table-core): consistent alphanumeric sorting for mixed letter/digit strings#6187

Open
isaackaara wants to merge 2 commits intoTanStack:mainfrom
isaackaara:fix/alphanumeric-sorting-consistency
Open

fix(table-core): consistent alphanumeric sorting for mixed letter/digit strings#6187
isaackaara wants to merge 2 commits intoTanStack:mainfrom
isaackaara:fix/alphanumeric-sorting-consistency

Conversation

@isaackaara
Copy link

Problem

The alphanumeric sorting function produces inconsistent ordering for mixed letter/digit strings depending on where digits appear in the string.

As reported in #6174:

  • At the start of a string: letters sort before digits (Aapple < 1apple) ✓
  • In the middle of a string: digits sort before letters (apple1 < appleA) ✗

Expected consistent behavior: letters should always sort before digits regardless of position.

Root Cause

The previous implementation used regex-based chunk splitting (/([0-9]+)/gm), which produced misaligned chunks when comparing strings with different digit/letter boundaries:

  • "apple1" splits to ["apple", "1"]
  • "appleA" splits to ["appleA"] (no digit groups)

This caused the comparison to be between "apple" and "appleA" (string comparison), rather than correctly comparing character-by-character at the divergence point.

Fix

Replaced the chunk-based approach with a character-by-character comparison that:

  1. Compares non-digit characters lexicographically — standard string ordering
  2. Extracts and compares full digit sequences as numbers — natural numeric ordering (e.g., 2 < 10)
  3. Consistently sorts letters before digits at any position — the key fix

Example sort results (ascending):

Before (inconsistent) After (consistent)
apple1 appleA
apple2 appleB
appleA apple1
appleB apple2

The exported reSplitAlphaNumeric regex is preserved (marked as deprecated) for backwards compatibility.

Closes #6174

…it strings

The previous regex-based chunk splitting approach produced inconsistent
sort order for mixed alphanumeric strings. When comparing 'apple1' vs
'appleA', the regex would split 'apple1' into ['apple', '1'] but
leave 'appleA' as one chunk ['appleA'], causing misaligned comparisons
where letters sorted before digits at the start of a string but after
digits in the middle.

This replaces the chunk-based approach with a character-by-character
comparison that:
- Compares non-digit characters lexicographically
- Extracts and compares full digit sequences as numbers
- Consistently sorts letters before digits at any position

The exported `reSplitAlphaNumeric` regex is preserved (deprecated) for
backwards compatibility.

Closes TanStack#6174
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: a3dce0f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/table-core Patch
@tanstack/angular-table Patch
@tanstack/lit-table Patch
@tanstack/qwik-table Patch
@tanstack/react-table Patch
@tanstack/solid-table Patch
@tanstack/svelte-table Patch
@tanstack/vue-table Patch
@tanstack/react-table-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a65bf2f8-2c33-4520-8578-423cbd6cee73

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant