From 97d85127c231d1b0823a3459d638dbd4f0ba1460 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Thu, 16 Feb 2017 14:30:39 +0100 Subject: [PATCH] Sort `docker stack ls` by name Signed-off-by: Vincent Demeester --- cli/command/stack/list.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cli/command/stack/list.go b/cli/command/stack/list.go index 9b6c645e29..3d81242b7a 100644 --- a/cli/command/stack/list.go +++ b/cli/command/stack/list.go @@ -3,17 +3,17 @@ package stack import ( "fmt" "io" + "sort" "strconv" "text/tabwriter" - "golang.org/x/net/context" - "github.com/docker/docker/api/types" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/compose/convert" "github.com/docker/docker/client" "github.com/spf13/cobra" + "golang.org/x/net/context" ) const ( @@ -53,12 +53,20 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { return nil } +type byName []*stack + +func (n byName) Len() int { return len(n) } +func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] } +func (n byName) Less(i, j int) bool { return n[i].Name < n[j].Name } + func printTable(out io.Writer, stacks []*stack) { writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0) // Ignore flushing errors defer writer.Flush() + sort.Sort(byName(stacks)) + fmt.Fprintf(writer, listItemFmt, "NAME", "SERVICES") for _, stack := range stacks { fmt.Fprintf(