我希望在Golang中排序一段IP地址(仅限IPV4)。
sort.Strings()sort192.168.4.41192.168.4.5
通过在映射中将IP的数值与IP字符串一起排序,我想出了一种实现它的方法,但它感觉太手动了。这是分解IP字符串并对地址进行排序的最有效方法吗?
package main
import (
"fmt"
"strconv"
"strings"
"sort"
)
func main() {
ips := []string{
"192.168.1.5",
"69.52.220.44",
"10.152.16.23",
"192.168.3.10",
"192.168.1.4",
"192.168.1.41",
}
ipsWithInt := make(map[string]int64)
for _, ip := range ips {
ipStr := strings.Split(ip, ".")
oct0, _ := strconv.ParseInt(ipStr[0], 10, 64)
ipInt0 := oct0 * 255 * 255 * 255
oct1, _ := strconv.ParseInt(ipStr[1], 10, 64)
ipInt1 := oct1 * 255 * 255
oct2, _ := strconv.ParseInt(ipStr[2], 10, 64)
ipInt2 := oct2 * 255
oct3, _ := strconv.ParseInt(ipStr[3], 10, 64)
ipInt3 := oct3
ipInt := ipInt0 + ipInt1 + ipInt2 + ipInt3
ipsWithInt[ip] = ipInt
}
type kv struct {
Key string
Value int64
}
var ss []kv
for k, v := range ipsWithInt {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value < ss[j].Value
})
for _, kv := range ss {
fmt.Printf("%s\n", kv.Key)
}
}
结果:
10.152.16.23
69.52.220.44
192.168.1.4
192.168.1.5
192.168.1.41
192.168.3.10