fix(base): correct natural sort of numbers with leading zeros (#38163)

### Description

`NaturalSortCompare` (`modules/base/natural_sort.go`) compares two
numeric run parts by **raw string length**:

```go
if len(part1) != len(part2) {
    return len(part1) - len(part2)
}
```

"Longer digit string = larger number" only holds without leading zeros.
With zero-padded numbers the comparison inverts:

- `file0001` vs `file2` → claims `file0001 > file2`, but `1 < 2`
- `a08` vs `a9` → claims `a08 > a9`, but `8 < 9`

This 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 `naturalSortTrimZeros` helper
(keeps one char so `"000"` → `"0"`).

Signed-off-by: Seonghyun Hong <s3onghyun.hong@gmail.com>
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
SEONGHYUN HONG
2026-08-22 18:40:49 +09:00
committed by GitHub
parent fa0b39a42b
commit 84b67d50a6
2 changed files with 22 additions and 3 deletions
+5
View File
@@ -40,4 +40,9 @@ func TestNaturalSortLess(t *testing.T) {
testLess("A-2", "A-11")
testLess("0.txt", "1.txt")
testLess("file0001", "file2")
testLess("a8", "a08")
testLess("00", "1")
testLess("0", "00") // equal value, fewer leading zeros sorts first
}