From 84b67d50a6a1fe14da89524fd7802b4eb7b6c180 Mon Sep 17 00:00:00 2001 From: SEONGHYUN HONG Date: Sat, 22 Aug 2026 18:40:49 +0900 Subject: [PATCH] fix(base): correct natural sort of numbers with leading zeros (#38163) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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 Signed-off-by: wxiaoguang Co-authored-by: silverwind Co-authored-by: wxiaoguang --- modules/base/natural_sort.go | 20 +++++++++++++++++--- modules/base/natural_sort_test.go | 5 +++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/base/natural_sort.go b/modules/base/natural_sort.go index d1ee7b04ec3..6a821f4d78a 100644 --- a/modules/base/natural_sort.go +++ b/modules/base/natural_sort.go @@ -4,6 +4,7 @@ package base import ( + "strings" "unicode/utf8" "golang.org/x/text/collate" @@ -41,6 +42,15 @@ func naturalSortAdvance(str string, pos int) (end int, isNumber bool) { return end, isNumber } +// naturalSortTrimZeros strips leading '0's, keeping one character so "000" collapses to "0" +func naturalSortTrimZeros(num string) string { + i := 0 + for i < len(num)-1 && num[i] == '0' { + i++ + } + return num[i:] +} + // NaturalSortCompare compares two strings so that they could be sorted in natural order func NaturalSortCompare(s1, s2 string) int { // There is a bug in Golang's collate package: https://github.com/golang/go/issues/67997 @@ -54,10 +64,14 @@ func NaturalSortCompare(s1, s2 string) int { part1, part2 := s1[pos1:end1], s2[pos2:end2] if isNum1 && isNum2 { if part1 != part2 { - if len(part1) != len(part2) { - return len(part1) - len(part2) + num1, num2 := naturalSortTrimZeros(part1), naturalSortTrimZeros(part2) + if len(num1) != len(num2) { + return len(num1) - len(num2) // without leading zeros, more digits means larger value } - return c.CompareString(part1, part2) + if cmp := strings.Compare(num1, num2); cmp != 0 { + return cmp // equal digit count, so byte order is numeric order + } + return len(part1) - len(part2) // equal value, fewer leading zeros sorts first } } else { if cmp := c.CompareString(part1, part2); cmp != 0 { diff --git a/modules/base/natural_sort_test.go b/modules/base/natural_sort_test.go index 451aba6618b..7e0815eee13 100644 --- a/modules/base/natural_sort_test.go +++ b/modules/base/natural_sort_test.go @@ -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 }