IP sort
This commit is contained in:
parent
7feba9845d
commit
3d6a83f76e
1 changed files with 35 additions and 6 deletions
|
|
@ -3,8 +3,41 @@ package main
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"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) {
|
func sort_hosts(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
sort_method := r.FormValue("sort_method")
|
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
|
return AllHosts[i].Name > AllHosts[j].Name
|
||||||
})
|
})
|
||||||
case "ip-up":
|
case "ip-up":
|
||||||
sort.SliceStable(AllHosts, func(i, j int) bool {
|
sort_by_ips("asc")
|
||||||
return AllHosts[i].Ip < AllHosts[j].Ip
|
|
||||||
})
|
|
||||||
case "ip-down":
|
case "ip-down":
|
||||||
sort.SliceStable(AllHosts, func(i, j int) bool {
|
sort_by_ips("desc")
|
||||||
return AllHosts[i].Ip > AllHosts[j].Ip
|
|
||||||
})
|
|
||||||
case "date-up":
|
case "date-up":
|
||||||
sort.SliceStable(AllHosts, func(i, j int) bool {
|
sort.SliceStable(AllHosts, func(i, j int) bool {
|
||||||
return AllHosts[i].Date < AllHosts[j].Date
|
return AllHosts[i].Date < AllHosts[j].Date
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue