history.go 841 B

123456789101112131415161718192021222324252627282930
  1. package container
  2. import "sort"
  3. // History is a convenience type for storing a list of containers,
  4. // sorted by creation date in descendant order.
  5. type History []*Container
  6. // Len returns the number of containers in the history.
  7. func (history *History) Len() int {
  8. return len(*history)
  9. }
  10. // Less compares two containers and returns true if the second one
  11. // was created before the first one.
  12. func (history *History) Less(i, j int) bool {
  13. containers := *history
  14. return containers[j].Created.Before(containers[i].Created)
  15. }
  16. // Swap switches containers i and j positions in the history.
  17. func (history *History) Swap(i, j int) {
  18. containers := *history
  19. containers[i], containers[j] = containers[j], containers[i]
  20. }
  21. // sort orders the history by creation date in descendant order.
  22. func (history *History) sort() {
  23. sort.Sort(history)
  24. }