-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.go
More file actions
49 lines (42 loc) · 1.38 KB
/
sort.go
File metadata and controls
49 lines (42 loc) · 1.38 KB
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
package main
import (
"cmp"
"slices"
"strings"
)
type RollupGetter[T cmp.Ordered] func(SmemRollup) T
type RollupComparator func(SmemRollup, SmemRollup) int
func makeComparator[T cmp.Ordered](getter RollupGetter[T]) RollupComparator {
return func(a, b SmemRollup) int {
return cmp.Compare(getter(a), getter(b))
}
}
// Sorts rollups by one of the supported keys.
// Keys are validated upstream.
// Helper abstractions:
// comparator function -- takes two SmemRollups and compares them
// getter function -- used by comparator internally to obtain values to feed into cmp.Compare
// comparator factory -- takes a getter function and returns a comparator function
func sortRollups(rollups []SmemRollup, pidOwnersMap map[int]PidOwner, cmdlineMap map[int]string, key string, reverseOrder bool) []SmemRollup {
comparators := map[string]RollupComparator{
"pid": makeComparator(SmemRollup.PID),
"uss": makeComparator(SmemRollup.USS),
"pss": makeComparator(SmemRollup.PSS),
"rss": makeComparator(SmemRollup.RSS),
"user": makeComparator(func(r SmemRollup) string {
return pidOwnersMap[r.PID()].username
}),
"command": makeComparator(func(r SmemRollup) string {
return cmdlineMap[r.PID()]
}),
}
comparator := comparators[strings.ToLower(key)]
slices.SortFunc(rollups, func(a, b SmemRollup) int {
c := comparator(a, b)
if reverseOrder {
c *= -1
}
return c
})
return rollups
}