api: Sort images by Created.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
This commit is contained in:
Dongsheng Yang 2015-12-15 10:00:52 +08:00
parent d3affe5904
commit 5aecaddf72
2 changed files with 25 additions and 1 deletions

View File

@ -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)
}

View File

@ -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
}