fix(base): correct natural sort of numbers with leading zeros#38163
Open
s3onghyun wants to merge 1 commit into
Open
fix(base): correct natural sort of numbers with leading zeros#38163s3onghyun wants to merge 1 commit into
s3onghyun wants to merge 1 commit into
Conversation
NaturalSortCompare compared numeric runs by raw byte length, which only reflects magnitude when there are no leading zeros. So 'file0001' sorted after 'file2' and 'a08' after 'a9'. Strip leading zeros before comparing digit count, then fall back to collation and original length. Signed-off-by: Seonghyun Hong <s3onghyun.hong@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
NaturalSortCompare(modules/base/natural_sort.go) compares two numeric run parts by raw string length:"Longer digit string = larger number" only holds without leading zeros. With zero-padded numbers the comparison inverts:
file0001vsfile2→ claimsfile0001 > file2, but1 < 2a08vsa9→ claimsa08 > a9, but8 < 9This affects any natural-ordered listing where zero-padded and shorter unpadded numbers mix (branch/tag/file names, etc.).
Fix
Strip leading zeros before comparing digit-count magnitude; on equal magnitude fall back to collation, then to the original length so fewer leading zeros sort first. Added a small
naturalSortTrimZeroshelper (keeps one char so"000"→"0").Testing
Added cases to
TestNaturalSortLess(natural_sort_test.go):file0001 < file2,a08 < a9,00 < 1, etc. Five cases fail against the original code, all pass after;go test ./modules/base/green.