Skip to content

Commit 918ec8c

Browse files
authored
Merge pull request #6595 from vvoland/list-tree-sortby-name
image/tree: Sort image tree by name instead of creation date
2 parents 513ee76 + 8e2943c commit 918ec8c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cli/command/image/tree.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"os"
10+
"slices"
1011
"sort"
1112
"strings"
1213

@@ -90,16 +91,36 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
9091

9192
details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
9293

94+
// Sort tags for this image
95+
sortedTags := make([]string, len(img.RepoTags))
96+
copy(sortedTags, img.RepoTags)
97+
slices.Sort(sortedTags)
98+
9399
view.images = append(view.images, topImage{
94-
Names: img.RepoTags,
100+
Names: sortedTags,
95101
Details: details,
96102
Children: children,
97103
created: img.Created,
98104
})
99105
}
100106

101-
sort.Slice(view.images, func(i, j int) bool {
102-
return view.images[i].created > view.images[j].created
107+
slices.SortFunc(view.images, func(a, b topImage) int {
108+
nameA := ""
109+
if len(a.Names) > 0 {
110+
nameA = a.Names[0]
111+
}
112+
nameB := ""
113+
if len(b.Names) > 0 {
114+
nameB = b.Names[0]
115+
}
116+
// Empty names sort last
117+
if (nameA == "") != (nameB == "") {
118+
if nameB == "" {
119+
return -1
120+
}
121+
return 1
122+
}
123+
return strings.Compare(nameA, nameB)
103124
})
104125

105126
return printImageTree(dockerCLI, view)

0 commit comments

Comments
 (0)