sorter.go 904 B

123456789101112131415161718192021222324252627282930313233343536
  1. package docker
  2. import "sort"
  3. type imageSorter struct {
  4. images []APIImages
  5. by func(i1, i2 *APIImages) bool // Closure used in the Less method.
  6. }
  7. // Len is part of sort.Interface.
  8. func (s *imageSorter) Len() int {
  9. return len(s.images)
  10. }
  11. // Swap is part of sort.Interface.
  12. func (s *imageSorter) Swap(i, j int) {
  13. s.images[i], s.images[j] = s.images[j], s.images[i]
  14. }
  15. // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
  16. func (s *imageSorter) Less(i, j int) bool {
  17. return s.by(&s.images[i], &s.images[j])
  18. }
  19. // Sort []ApiImages by most recent creation date and tag name.
  20. func sortImagesByCreationAndTag(images []APIImages) {
  21. creationAndTag := func(i1, i2 *APIImages) bool {
  22. return i1.Created > i2.Created || (i1.Created == i2.Created && i2.Tag > i1.Tag)
  23. }
  24. sorter := &imageSorter{
  25. images: images,
  26. by: creationAndTag}
  27. sort.Sort(sorter)
  28. }