mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"sort"
|
|
)
|
|
|
|
type ByID []*Container
|
|
|
|
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 }
|
|
|
|
type ByName []*Container
|
|
|
|
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].id < a[j].id }
|
|
|
|
type ByCPU []*Container
|
|
|
|
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].reader.CPUUtil < a[j].reader.CPUUtil }
|
|
|
|
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].reader.MemUsage < a[j].reader.MemUsage }
|
|
|
|
// Return array of containers, sorted by field
|
|
func (cm *ContainerMap) Sorted() []*Container {
|
|
containers := cm.All()
|
|
|
|
switch cm.sortField {
|
|
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)))
|
|
default:
|
|
sort.Sort(ByID(containers))
|
|
}
|
|
|
|
return containers
|
|
}
|