-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorting.go
66 lines (56 loc) · 1.36 KB
/
sorting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"github.com/miekg/dns"
)
// CanonicalDomainOrder compares 2 domain name strings in DNS canonical order,
// and returns -1, 0, or 1, according to whether the first string sorts earlier
// than, equal to, or later than the second string.
func CanonicalDomainOrder(d1, d2 string) int {
name1, name2 := dns.CanonicalName(dns.Fqdn(d1)), dns.CanonicalName(dns.Fqdn(d2))
if name1 == name2 {
return 0
}
labels1, labels2 := dns.SplitDomainName(name1), dns.SplitDomainName(name2)
len1, len2 := len(labels1), len(labels2)
var minlength int
if len1 <= len2 {
minlength = len1
} else {
minlength = len2
}
for i := 0; i < minlength; i++ {
l1, l2 := labels1[len1-i-1], labels2[len2-i-1]
if l1 > l2 {
return 1
} else if l2 > l1 {
return -1
}
}
if len1 > len2 {
return 1
} else if len2 > len1 {
return -1
}
return 0
}
type ByCanonicalOrder []string
func (s ByCanonicalOrder) Len() int {
return len(s)
}
func (s ByCanonicalOrder) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByCanonicalOrder) Less(i, j int) bool {
return CanonicalDomainOrder(s[i], s[j]) == -1
}
// To sort Response lists by version (IPv6 first)
type ByIPversion []Response
func (s ByIPversion) Len() int {
return len(s)
}
func (s ByIPversion) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByIPversion) Less(i, j int) bool {
return s[i].ip.To4() == nil
}