From 3d6a83f76e33c166beb7fefc16a5ef57a1ff6b65 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Wed, 14 Sep 2022 20:35:42 +0700 Subject: [PATCH] IP sort --- src/web-sort.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/web-sort.go b/src/web-sort.go index eaa9e40..e6e5fa0 100644 --- a/src/web-sort.go +++ b/src/web-sort.go @@ -3,8 +3,41 @@ package main import ( "net/http" "sort" + "bytes" + "net" ) +func sort_by_ips(method string) { + type ipHost struct { + Host Host + Ip net.IP + } + toSort := []ipHost{} + var oneSort ipHost + + for _, oneHost := range AllHosts { + oneSort.Host = oneHost + oneSort.Ip = net.ParseIP(oneHost.Ip) + toSort = append(toSort, oneSort) + } + + switch method { + case "asc": + sort.Slice(toSort, func(i, j int) bool { + return bytes.Compare(toSort[i].Ip, toSort[j].Ip) < 0 + }) + case "desc": + sort.Slice(toSort, func(i, j int) bool { + return bytes.Compare(toSort[i].Ip, toSort[j].Ip) > 0 + }) + } + + AllHosts = []Host{} + for _, oneSort := range toSort { + AllHosts = append(AllHosts, oneSort.Host) + } +} + func sort_hosts(w http.ResponseWriter, r *http.Request) { sort_method := r.FormValue("sort_method") @@ -19,13 +52,9 @@ func sort_hosts(w http.ResponseWriter, r *http.Request) { return AllHosts[i].Name > AllHosts[j].Name }) case "ip-up": - sort.SliceStable(AllHosts, func(i, j int) bool { - return AllHosts[i].Ip < AllHosts[j].Ip - }) + sort_by_ips("asc") case "ip-down": - sort.SliceStable(AllHosts, func(i, j int) bool { - return AllHosts[i].Ip > AllHosts[j].Ip - }) + sort_by_ips("desc") case "date-up": sort.SliceStable(AllHosts, func(i, j int) bool { return AllHosts[i].Date < AllHosts[j].Date