all: use slices.Sort() to sort strings (#1712)

This commit is contained in:
Daniel Liu 2025-12-07 18:19:13 +08:00 committed by GitHub
parent 1990e73b3c
commit eaaeea0cad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 39 additions and 40 deletions

View file

@ -23,7 +23,6 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"slices" "slices"
"sort"
"strings" "strings"
"text/template" "text/template"
"unicode" "unicode"
@ -280,7 +279,7 @@ func iterSorted[V any](inp map[string]V, onItem func(string, V) error) error {
for key := range inp { for key := range inp {
sortedKeys = append(sortedKeys, key) sortedKeys = append(sortedKeys, key)
} }
sort.Strings(sortedKeys) slices.Sort(sortedKeys)
for _, key := range sortedKeys { for _, key := range sortedKeys {
if err := onItem(key, inp[key]); err != nil { if err := onItem(key, inp[key]); err != nil {

View file

@ -24,7 +24,7 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -53,7 +53,7 @@ func (c config) servers() []string {
for server := range c.Servers { for server := range c.Servers {
servers = append(servers, server) servers = append(servers, server)
} }
sort.Strings(servers) slices.Sort(servers)
return servers return servers
} }

View file

@ -18,7 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"sort" "slices"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
) )
@ -98,7 +98,7 @@ func (w *wizard) deployEthstats() {
} }
break break
} }
sort.Strings(infos.banned) slices.Sort(infos.banned)
} }
} }
// Try to deploy the ethstats server on the host // Try to deploy the ethstats server on the host

View file

@ -19,7 +19,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"sort" "slices"
"strings" "strings"
"sync" "sync"
@ -226,7 +226,7 @@ func (stats serverStats) render() {
for server := range stats { for server := range stats {
servers = append(servers, server) servers = append(servers, server)
} }
sort.Strings(servers) slices.Sort(servers)
for i, server := range servers { for i, server := range servers {
// Add a separator between all servers // Add a separator between all servers
@ -238,7 +238,7 @@ func (stats serverStats) render() {
for service := range stats[server].services { for service := range stats[server].services {
services = append(services, service) services = append(services, service)
} }
sort.Strings(services) slices.Sort(services)
if len(services) == 0 { if len(services) == 0 {
table.Append([]string{server, stats[server].address, "", "", ""}) table.Append([]string{server, stats[server].address, "", "", ""})
@ -253,7 +253,7 @@ func (stats serverStats) render() {
for service := range stats[server].services[service] { for service := range stats[server].services[service] {
configs = append(configs, service) configs = append(configs, service)
} }
sort.Strings(configs) slices.Sort(configs)
for k, config := range configs { for k, config := range configs {
switch { switch {

View file

@ -24,7 +24,7 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -316,7 +316,7 @@ func (c *Console) Welcome() {
for api, version := range apis { for api, version := range apis {
modules = append(modules, fmt.Sprintf("%s:%s", api, version)) modules = append(modules, fmt.Sprintf("%s:%s", api, version))
} }
sort.Strings(modules) slices.Sort(modules)
message += " modules: " + strings.Join(modules, " ") + "\n" message += " modules: " + strings.Join(modules, " ") + "\n"
} }
message += "\nTo exit, press ctrl-d or type exit" message += "\nTo exit, press ctrl-d or type exit"

View file

@ -19,7 +19,7 @@ package dbtest
import ( import (
"bytes" "bytes"
"reflect" "reflect"
"sort" "slices"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/ethdb" "github.com/XinFinOrg/XDPoSChain/ethdb"
@ -133,7 +133,7 @@ func TestDatabaseSuite(t *testing.T, New func() ethdb.KeyValueStore) {
defer db.Close() defer db.Close()
keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"} keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"}
sort.Strings(keys) // 1, 10, 11, etc slices.Sort(keys) // 1, 10, 11, etc
for _, k := range keys { for _, k := range keys {
if err := db.Put([]byte(k), nil); err != nil { if err := db.Put([]byte(k), nil); err != nil {
@ -319,7 +319,7 @@ func iterateKeys(it ethdb.Iterator) []string {
for it.Next() { for it.Next() {
keys = append(keys, string(it.Key())) keys = append(keys, string(it.Key()))
} }
sort.Strings(keys) slices.Sort(keys)
it.Release() it.Release()
return keys return keys
} }

View file

@ -19,7 +19,7 @@ package memorydb
import ( import (
"errors" "errors"
"sort" "slices"
"strings" "strings"
"sync" "sync"
@ -160,7 +160,7 @@ func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
} }
} }
// Sort the items and retrieve the associated values // Sort the items and retrieve the associated values
sort.Strings(keys) slices.Sort(keys)
for _, key := range keys { for _, key := range keys {
values = append(values, db.db[key]) values = append(values, db.db[key])
} }

View file

@ -21,7 +21,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"sort" "slices"
"strings" "strings"
) )
@ -71,6 +71,6 @@ func DiffHashes(a map[string][32]byte, b map[string][32]byte) []string {
updates = append(updates, file) updates = append(updates, file)
} }
} }
sort.Strings(updates) slices.Sort(updates)
return updates return updates
} }

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"github.com/XinFinOrg/XDPoSChain/internal/version" "github.com/XinFinOrg/XDPoSChain/internal/version"
@ -277,7 +277,7 @@ func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) {
} }
} }
keyvals := os.Environ() keyvals := os.Environ()
sort.Strings(keyvals) slices.Sort(keyvals)
for _, keyval := range keyvals { for _, keyval := range keyvals {
key := strings.Split(keyval, "=")[0] key := strings.Split(keyval, "=")[0]

View file

@ -18,7 +18,7 @@ package jsre
import ( import (
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"github.com/dop251/goja" "github.com/dop251/goja"
@ -88,6 +88,6 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
} }
} }
sort.Strings(results) slices.Sort(results)
return results return results
} }

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
@ -221,8 +221,8 @@ func (ctx ppctx) fields(obj *goja.Object) []string {
} }
} }
iterOwnAndConstructorKeys(ctx.vm, obj, add) iterOwnAndConstructorKeys(ctx.vm, obj, add)
sort.Strings(vals) slices.Sort(vals)
sort.Strings(methods) slices.Sort(methods)
return append(vals, methods...) return append(vals, methods...)
} }

View file

@ -19,7 +19,7 @@ package prometheus
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
@ -144,7 +144,7 @@ func (c *collector) writeGaugeInfo(name string, value metrics.GaugeInfoValue) {
for k, v := range value { for k, v := range value {
kvs = append(kvs, fmt.Sprintf("%v=%q", k, v)) kvs = append(kvs, fmt.Sprintf("%v=%q", k, v))
} }
sort.Strings(kvs) slices.Sort(kvs)
c.buff.WriteString(fmt.Sprintf("{%v} 1\n\n", strings.Join(kvs, ", "))) c.buff.WriteString(fmt.Sprintf("{%v} 1\n\n", strings.Join(kvs, ", ")))
} }

View file

@ -20,7 +20,7 @@ package prometheus
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sort" "slices"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/metrics" "github.com/XinFinOrg/XDPoSChain/metrics"
@ -34,7 +34,7 @@ func Handler(reg metrics.Registry) http.Handler {
reg.Each(func(name string, i interface{}) { reg.Each(func(name string, i interface{}) {
names = append(names, name) names = append(names, name)
}) })
sort.Strings(names) slices.Sort(names)
// Aggregate all the metrics into a Prometheus collector // Aggregate all the metrics into a Prometheus collector
c := newCollector() c := newCollector()

View file

@ -3,7 +3,7 @@ package metrics
import ( import (
"errors" "errors"
"fmt" "fmt"
"sort" "slices"
"strings" "strings"
"sync" "sync"
) )
@ -54,7 +54,7 @@ func (r *orderedRegistry) Each(f func(string, interface{})) {
for name := range reg { for name := range reg {
names = append(names, name) names = append(names, name)
} }
sort.Strings(names) slices.Sort(names)
for _, name := range names { for _, name := range names {
f(name, reg[name]) f(name, reg[name])
} }

View file

@ -24,7 +24,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -183,7 +183,7 @@ func (h *httpServer) start() error {
for path := range h.handlerNames { for path := range h.handlerNames {
paths = append(paths, path) paths = append(paths, path)
} }
sort.Strings(paths) slices.Sort(paths)
logged := make(map[string]bool, len(paths)) logged := make(map[string]bool, len(paths))
for _, path := range paths { for _, path := range paths {
name := h.handlerNames[path] name := h.handlerNames[path]

View file

@ -22,7 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"sort" "slices"
"strings" "strings"
) )
@ -303,7 +303,7 @@ func (s DistinctNetSet) String() string {
for k := range s.members { for k := range s.members {
keys = append(keys, k) keys = append(keys, k)
} }
sort.Strings(keys) slices.Sort(keys)
for i, k := range keys { for i, k := range keys {
var ip net.IP var ip net.IP
if k[0] == '4' { if k[0] == '4' {

View file

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"go/format" "go/format"
"go/types" "go/types"
"sort" "slices"
"github.com/XinFinOrg/XDPoSChain/rlp/internal/rlpstruct" "github.com/XinFinOrg/XDPoSChain/rlp/internal/rlpstruct"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
@ -194,7 +194,7 @@ func (ctx *genContext) importsList() []string {
imp = append(imp, fmt.Sprintf("%s %q", p.alias, path)) imp = append(imp, fmt.Sprintf("%s %q", p.alias, path))
} }
} }
sort.Strings(imp) slices.Sort(imp)
return imp return imp
} }

View file

@ -25,7 +25,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"testing" "testing"
@ -261,7 +261,7 @@ func sortedMapKeys(m reflect.Value) []string {
for i, k := range m.MapKeys() { for i, k := range m.MapKeys() {
keys[i] = k.String() keys[i] = k.String()
} }
sort.Strings(keys) slices.Sort(keys)
return keys return keys
} }