diff --git a/api/handlers.go b/api/handlers.go index 02ffed3965..68ade28b5b 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -181,6 +181,7 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) { images = append(images, image) } + sort.Sort(sort.Reverse(ImageSorter(images))) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(images) } diff --git a/api/sorter.go b/api/sorter.go index b16350eeab..fbc99cee2e 100644 --- a/api/sorter.go +++ b/api/sorter.go @@ -1,6 +1,9 @@ package api -import "github.com/docker/swarm/cluster" +import ( + "github.com/docker/swarm/cluster" + "github.com/samalba/dockerclient" +) // ContainerSorter implements the Sort interface to sort Docker containers. // It is not guaranteed to be a stable sort. @@ -21,3 +24,23 @@ func (s ContainerSorter) Swap(i, j int) { func (s ContainerSorter) Less(i, j int) bool { return s[i].Info.Created < s[j].Info.Created } + +// ImageSorter implements the Sort interface to sort Docker Images. +// It is not guaranteed to be a stable sort. +type ImageSorter []dockerclient.Image + +// Len returns the number of images to be sorted. +func (s ImageSorter) Len() int { + return len(s) +} + +// Swap exchanges the container elements with indices i and j. +func (s ImageSorter) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Less reports whether the Image with index i should sort before the Image with index j. +// Images are sorted chronologically by when they were created. +func (s ImageSorter) Less(i, j int) bool { + return s[i].Created < s[j].Created +}