-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortable_containers.go
78 lines (71 loc) · 2.5 KB
/
sortable_containers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import "strconv"
type SortableContainers struct {
containers []Container
sort Sort // property to sort on
reverse bool // sort is reversed
}
func (a SortableContainers) Len() int { return len(a.containers) }
func (a SortableContainers) Swap(i, j int) {
a.containers[i], a.containers[j] = a.containers[j], a.containers[i]
}
func (a SortableContainers) Less(i, j int) bool {
var less bool
switch a.sort {
case SORT_NAME:
if a.containers[i].container.Name == a.containers[j].container.Name {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
less = a.containers[i].container.Name < a.containers[j].container.Name
}
case SORT_IMAGE:
if a.containers[i].container.Image == a.containers[j].container.Image {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
less = a.containers[i].container.Image < a.containers[j].container.Image
}
case SORT_ID:
// always unique
less = a.containers[i].container.Id < a.containers[j].container.Id
case SORT_COMMAND:
if a.containers[i].container.Command == a.containers[j].container.Command {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
less = a.containers[i].container.Command < a.containers[j].container.Command
}
case SORT_UPTIME:
if a.containers[i].container.Uptime == a.containers[j].container.Uptime {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
less = a.containers[i].container.Uptime < a.containers[j].container.Uptime
}
case SORT_CPU:
if a.containers[i].container.CPU == a.containers[j].container.CPU {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
cpu_a, _ := strconv.ParseFloat(a.containers[i].container.CPU, 32)
cpu_b, _ := strconv.ParseFloat(a.containers[j].container.CPU, 32)
less = cpu_a > cpu_b
}
case SORT_RAM:
if a.containers[i].container.RAM == a.containers[j].container.RAM {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
ram_a, _ := strconv.ParseFloat(a.containers[i].container.RAM, 32)
ram_b, _ := strconv.ParseFloat(a.containers[j].container.RAM, 32)
less = ram_a > ram_b
}
case SORT_STATUS:
if a.containers[i].container.Status == a.containers[j].container.Status {
less = a.containers[i].container.Id < a.containers[j].container.Id
} else {
less = a.containers[i].container.Status < a.containers[j].container.Status
}
default:
less = i < j
}
if a.reverse {
less = !less
}
return less
}