diff --git a/src/go/ast/import.go b/src/go/ast/import.go index 6b27fe822e..be23c7fc43 100644 --- a/src/go/ast/import.go +++ b/src/go/ast/import.go @@ -123,14 +123,14 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec { comments := f.Comments[cstart:cend] // Assign each comment to the import spec preceding it. - importComment := map[*ImportSpec][]*CommentGroup{} + importComments := map[*ImportSpec][]*CommentGroup{} specIndex := 0 for _, g := range comments { for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() { specIndex++ } s := specs[specIndex].(*ImportSpec) - importComment[s] = append(importComment[s], g) + importComments[s] = append(importComments[s], g) } // Sort the import specs by import path. @@ -138,7 +138,19 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec { // Reassign the import paths to have the same position sequence. // Reassign each comment to abut the end of its spec. // Sort the comments by new position. - sort.Sort(byImportSpec(specs)) + sort.Slice(specs, func(i, j int) bool { + ipath := importPath(specs[i]) + jpath := importPath(specs[j]) + if ipath != jpath { + return ipath < jpath + } + iname := importName(specs[i]) + jname := importName(specs[j]) + if iname != jname { + return iname < jname + } + return importComment(specs[i]) < importComment(specs[j]) + }) // Dedup. Thanks to our sorting, we can just consider // adjacent pairs of imports. @@ -161,38 +173,16 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec { } s.Path.ValuePos = pos[i].Start s.EndPos = pos[i].End - for _, g := range importComment[s] { + for _, g := range importComments[s] { for _, c := range g.List { c.Slash = pos[i].End } } } - sort.Sort(byCommentPos(comments)) + sort.Slice(comments, func(i, j int) bool { + return comments[i].Pos() < comments[j].Pos() + }) return specs } - -type byImportSpec []Spec // slice of *ImportSpec - -func (x byImportSpec) Len() int { return len(x) } -func (x byImportSpec) Swap(i, j int) { x[i], x[j] = x[j], x[i] } -func (x byImportSpec) Less(i, j int) bool { - ipath := importPath(x[i]) - jpath := importPath(x[j]) - if ipath != jpath { - return ipath < jpath - } - iname := importName(x[i]) - jname := importName(x[j]) - if iname != jname { - return iname < jname - } - return importComment(x[i]) < importComment(x[j]) -} - -type byCommentPos []*CommentGroup - -func (x byCommentPos) Len() int { return len(x) } -func (x byCommentPos) Swap(i, j int) { x[i], x[j] = x[j], x[i] } -func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() } diff --git a/src/go/doc/example.go b/src/go/doc/example.go index bbf8096ce2..a89f29b40f 100644 --- a/src/go/doc/example.go +++ b/src/go/doc/example.go @@ -94,7 +94,10 @@ func Examples(files ...*ast.File) []*Example { } list = append(list, flist...) } - sort.Sort(exampleByName(list)) + // sort by name + sort.Slice(list, func(i, j int) bool { + return list[i].Name < list[j].Name + }) return list } @@ -135,12 +138,6 @@ func isTest(name, prefix string) bool { return !unicode.IsLower(rune) } -type exampleByName []*Example - -func (s exampleByName) Len() int { return len(s) } -func (s exampleByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s exampleByName) Less(i, j int) bool { return s[i].Name < s[j].Name } - // playExample synthesizes a new *ast.File based on the provided // file with the provided function body as the body of main. func playExample(file *ast.File, body *ast.BlockStmt) *ast.File { diff --git a/src/go/types/methodset.go b/src/go/types/methodset.go index 4f791d9d51..2a8b1c24f7 100644 --- a/src/go/types/methodset.go +++ b/src/go/types/methodset.go @@ -190,7 +190,10 @@ func NewMethodSet(T Type) *MethodSet { list = append(list, m) } } - sort.Sort(byUniqueName(list)) + // sort by unique name + sort.Slice(list, func(i, j int) bool { + return list[i].obj.Id() < list[j].obj.Id() + }) return &MethodSet{list} } @@ -257,10 +260,3 @@ func ptrRecv(f *Func) bool { _, isPtr := deref(f.typ.(*Signature).recv.typ) return isPtr } - -// byUniqueName function lists can be sorted by their unique names. -type byUniqueName []*Selection - -func (a byUniqueName) Len() int { return len(a) } -func (a byUniqueName) Less(i, j int) bool { return a[i].obj.Id() < a[j].obj.Id() } -func (a byUniqueName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }