diff --git a/src/cmd/distpack/archive.go b/src/cmd/distpack/archive.go index f731b3792f8c9f..8245381f901493 100644 --- a/src/cmd/distpack/archive.go +++ b/src/cmd/distpack/archive.go @@ -91,13 +91,29 @@ func (a *Archive) Add(name, src string, info fs.FileInfo) { }) } +func nameLess(x, y string) bool { + for i := 0; i < len(x) && i < len(y); i++ { + if x[i] != y[i] { + // foo/bar/baz before foo/bar.go, because foo/bar is before foo/bar.go + if x[i] == '/' { + return true + } + if y[i] == '/' { + return false + } + return x[i] < y[i] + } + } + return len(x) < len(y) +} + // Sort sorts the files in the archive. // It is only necessary to call Sort after calling Add or RenameGoMod. -// ArchiveDir returns a sorted archive, and the other methods +// NewArchive returns a sorted archive, and the other methods // preserve the sorting of the archive. func (a *Archive) Sort() { sort.Slice(a.Files, func(i, j int) bool { - return a.Files[i].Name < a.Files[j].Name + return nameLess(a.Files[i].Name, a.Files[j].Name) }) }