From d5ea22b602737fab88b062b76e92333c5adf3723 Mon Sep 17 00:00:00 2001 From: test Date: Sun, 26 Aug 2018 02:02:23 +0000 Subject: [PATCH] sort the program in xml request --- supervisor.go | 1 + types/process-name-sorter.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 types/process-name-sorter.go diff --git a/supervisor.go b/supervisor.go index 99267c3e..68ed5dbb 100644 --- a/supervisor.go +++ b/supervisor.go @@ -191,6 +191,7 @@ func (s *Supervisor) GetAllProcessInfo(r *http.Request, args *struct{}, reply *s procInfo := getProcessInfo(proc) reply.AllProcessInfo = append(reply.AllProcessInfo, *procInfo) }) + types.SortProcessInfos( reply.AllProcessInfo ) return nil } diff --git a/types/process-name-sorter.go b/types/process-name-sorter.go new file mode 100644 index 00000000..811aaa4d --- /dev/null +++ b/types/process-name-sorter.go @@ -0,0 +1,33 @@ +package types + +import ( + "sort" + "strings" +) + +type ProcessNameSorter struct { + processes []ProcessInfo +} + +func NewwProcessNameSorter(processes []ProcessInfo) *ProcessNameSorter { + return &ProcessNameSorter{processes: processes} +} + +func (pns *ProcessNameSorter) Len() int { + return len(pns.processes) +} + +func (pns *ProcessNameSorter) Less(i, j int) bool { + return strings.Compare(pns.processes[i].Name, pns.processes[j].Name) < 0 +} + +func (pns *ProcessNameSorter) Swap(i, j int) { + info := pns.processes[i] + pns.processes[i] = pns.processes[j] + pns.processes[j] = info +} + +func SortProcessInfos(processes []ProcessInfo) { + sorter := NewwProcessNameSorter(processes) + sort.Sort(sorter) +}