fix(table-core): consistent alphanumeric sorting for mixed letter/digit strings#6187
fix(table-core): consistent alphanumeric sorting for mixed letter/digit strings#6187isaackaara wants to merge 2 commits intoTanStack:mainfrom
Conversation
…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 detectedLatest commit: a3dce0f The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
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 |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
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:
Aapple<1apple) ✓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:
2<10)Example sort results (ascending):
The exported
reSplitAlphaNumericregex is preserved (marked as deprecated) for backwards compatibility.Closes #6174