ctop/sort.go

56 lines
1.6 KiB
Go
Raw Normal View History

2016-12-30 03:22:25 +00:00
package main
import (
"sort"
)
2017-01-06 12:02:56 +00:00
var SortFields = []string{"id", "name", "cpu", "mem", "mem %"}
2017-01-03 17:37:09 +00:00
// Sort array of containers by field
func SortContainers(field string, containers []*Container) {
switch field {
case "id":
sort.Sort(ByID(containers))
case "name":
sort.Sort(ByName(containers))
case "cpu":
sort.Sort(sort.Reverse(ByCPU(containers)))
case "mem":
sort.Sort(sort.Reverse(ByMem(containers)))
2017-01-06 12:02:56 +00:00
case "mem %":
sort.Sort(sort.Reverse(ByMemPercent(containers)))
2017-01-03 17:37:09 +00:00
default:
sort.Sort(ByID(containers))
}
}
2016-12-30 22:17:46 +00:00
type ByID []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
func (a ByID) Len() int { return len(a) }
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByID) Less(i, j int) bool { return a[i].id < a[j].id }
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
type ByName []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].name < a[j].name }
2016-12-30 03:22:25 +00:00
2016-12-30 22:17:46 +00:00
type ByCPU []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
func (a ByCPU) Len() int { return len(a) }
func (a ByCPU) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCPU) Less(i, j int) bool { return a[i].metrics.CPUUtil < a[j].metrics.CPUUtil }
2016-12-30 22:17:46 +00:00
type ByMem []*Container
func (a ByMem) Len() int { return len(a) }
func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMem) Less(i, j int) bool { return a[i].metrics.MemUsage < a[j].metrics.MemUsage }
2017-01-06 12:02:56 +00:00
type ByMemPercent []*Container
func (a ByMemPercent) Len() int { return len(a) }
func (a ByMemPercent) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMemPercent) Less(i, j int) bool { return a[i].metrics.MemPercent < a[j].metrics.MemPercent }