Browse Source

sort.Interface for CIDR list

Darien Raymond 7 years ago
parent
commit
ee74256aac
1 changed files with 35 additions and 0 deletions
  1. 35 0
      app/router/config.go

+ 35 - 0
app/router/config.go

@@ -6,6 +6,41 @@ import (
 	"v2ray.com/core/common/net"
 )
 
+type CIDRList []*CIDR
+
+func (l *CIDRList) Len() int {
+	return len(*l)
+}
+
+func (l *CIDRList) Less(i int, j int) bool {
+	ci := (*l)[i]
+	cj := (*l)[j]
+
+	if len(ci.Ip) < len(cj.Ip) {
+		return true
+	}
+
+	if len(ci.Ip) > len(cj.Ip) {
+		return false
+	}
+
+	for k := 0; k < len(ci.Ip); k++ {
+		if ci.Ip[k] < cj.Ip[k] {
+			return true
+		}
+
+		if ci.Ip[k] > cj.Ip[k] {
+			return false
+		}
+	}
+
+	return ci.Prefix < cj.Prefix
+}
+
+func (l *CIDRList) Swap(i int, j int) {
+	(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
+}
+
 type Rule struct {
 	Tag       string
 	Condition Condition