Skip to content

Commit f33afb4

Browse files
committed
refactor: replace sort with slices for improved sorting functionality
1 parent 69df5ec commit f33afb4

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

src/cmd/cli/command/commands.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"regexp"
12-
"sort"
12+
"slices"
1313
"strings"
1414
"time"
1515

@@ -443,7 +443,9 @@ var tenantsCmd = &cobra.Command{
443443
}
444444

445445
// Sort by name for stable output
446-
sort.Slice(tenants, func(i, j int) bool { return strings.ToLower(tenants[i].Name) < strings.ToLower(tenants[j].Name) })
446+
slices.SortFunc(tenants, func(a, b auth.Tenant) int {
447+
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
448+
})
447449

448450
if len(tenants) == 0 {
449451
term.Info("No tenants found")

src/pkg/cli/compose/validation.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path/filepath"
99
"regexp"
1010
"slices"
11-
"sort"
1211
"strconv"
1312
"strings"
1413
"time"
@@ -38,8 +37,8 @@ func ValidateProject(project *composeTypes.Project) error {
3837
for _, svccfg := range project.Services {
3938
services = append(services, svccfg)
4039
}
41-
sort.Slice(services, func(i, j int) bool {
42-
return services[i].Name < services[j].Name
40+
slices.SortFunc(services, func(a, b composeTypes.ServiceConfig) int {
41+
return strings.Compare(a.Name, b.Name)
4342
})
4443

4544
var errs []error

src/pkg/cli/deploymentsList.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cli
22

33
import (
44
"context"
5-
"sort"
5+
"slices"
66
"strings"
77
"time"
88

@@ -60,8 +60,8 @@ func DeploymentsList(ctx context.Context, listType defangv1.DeploymentType, proj
6060
// TODO: allow user to specify sort order
6161
sortKeys[i] = strings.Join([]string{d.ProjectName, d.Provider, d.AccountId, d.Region}, "|")
6262
}
63-
sort.SliceStable(sortKeys, func(i, j int) bool {
64-
return sortKeys[i] < sortKeys[j]
63+
slices.SortStableFunc(sortKeys, func(a, b string) int {
64+
return strings.Compare(a, b)
6565
})
6666

6767
return term.Table(deployments, []string{"ProjectName", "Provider", "AccountId", "Region", "Deployment", "DeployedAt"})

src/pkg/cli/estimate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9-
"sort"
9+
"slices"
1010
"strconv"
1111
"strings"
1212
"time"
@@ -165,8 +165,8 @@ func prepareEstimateLineItemTableItems(lineItems []*defangv1.EstimateLineItem) [
165165
}
166166

167167
// sort line items by service + description
168-
sort.Slice(tableItems, func(i, j int) bool {
169-
return tableItems[i].Service+tableItems[i].Description < tableItems[j].Service+tableItems[j].Description
168+
slices.SortFunc(tableItems, func(a, b EstimateLineItemTableItem) int {
169+
return strings.Compare(a.Service+a.Description, b.Service+b.Description)
170170
})
171171

172172
return tableItems

src/pkg/dns/resolver.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"net"
88
"slices"
9-
"sort"
9+
"strings"
1010

1111
"github.com/DefangLabs/defang/src/pkg"
1212
"github.com/miekg/dns"
@@ -76,7 +76,9 @@ func FindNSServers(ctx context.Context, domain string) ([]*net.NS, error) {
7676
index := pkg.RandomIndex(len(nsServers))
7777
nsServer := nsServers[index].Host
7878
ns, err := ResolverAt(nsServer).LookupNS(ctx, domain)
79-
sort.Slice(ns, func(i, j int) bool { return ns[i].Host < ns[j].Host })
79+
slices.SortFunc(ns, func(a, b *net.NS) int {
80+
return strings.Compare(a.Host, b.Host)
81+
})
8082
if err != nil {
8183
if retries--; retries > 0 {
8284
continue

0 commit comments

Comments
 (0)