diff --git a/api/client/network.go b/api/client/network.go index cefa7449f5..f992af3da4 100644 --- a/api/client/network.go +++ b/api/client/network.go @@ -3,6 +3,7 @@ package client import ( "fmt" "net" + "sort" "strings" "text/tabwriter" @@ -194,7 +195,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error { if !*quiet { fmt.Fprintln(wr, "NETWORK ID\tNAME\tDRIVER") } - + sort.Sort(byNetworkName(networkResources)) for _, networkResource := range networkResources { ID := networkResource.ID netName := networkResource.Name @@ -216,6 +217,12 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error { return nil } +type byNetworkName []types.NetworkResource + +func (r byNetworkName) Len() int { return len(r) } +func (r byNetworkName) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r byNetworkName) Less(i, j int) bool { return r[i].Name < r[j].Name } + // CmdNetworkInspect inspects the network object for more details // // Usage: docker network inspect [OPTIONS] [NETWORK...] diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index c15164bbf5..5d91bc459d 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -10,7 +10,6 @@ import ( "net/http" "net/http/httptest" "os" - "sort" "strings" "time" @@ -259,9 +258,6 @@ func assertNwList(c *check.C, out string, expectNws []string) { // wrap all network name in nwList nwList = append(nwList, netFields[1]) } - // first need to sort out and expected - sort.StringSlice(nwList).Sort() - sort.StringSlice(expectNws).Sort() // network ls should contains all expected networks c.Assert(nwList, checker.DeepEquals, expectNws) @@ -321,11 +317,11 @@ func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) { // filter with partial ID and partial name // only show 'bridge' and 'dev' network out, _ = dockerCmd(c, "network", "ls", "-f", "id="+networkID[0:5], "-f", "name=dge") - assertNwList(c, out, []string{"dev", "bridge"}) + assertNwList(c, out, []string{"bridge", "dev"}) // only show built-in network (bridge, none, host) out, _ = dockerCmd(c, "network", "ls", "-f", "type=builtin") - assertNwList(c, out, []string{"bridge", "none", "host"}) + assertNwList(c, out, []string{"bridge", "host", "none"}) // only show custom networks (dev) out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom") @@ -334,7 +330,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) { // show all networks with filter // it should be equivalent of ls without option out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom", "-f", "type=builtin") - assertNwList(c, out, []string{"dev", "bridge", "host", "none"}) + assertNwList(c, out, []string{"bridge", "dev", "host", "none"}) } func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {