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 (
|
||||
"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
|
||||
|
|
|
|||
Loading…
Reference in a new issue