File: sortip.go

package info (click to toggle)
golang-github-malfunkt-iprange 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 116 kB
  • sloc: yacc: 86; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 402 bytes parent folder | download | duplicates (2)
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
package iprange

import (
	"math/big"
	"net"
)

// Asc implements sorting in ascending order for IP addresses
type asc []net.IP

func (a asc) Len() int {
	return len(a)
}

func (a asc) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a asc) Less(i, j int) bool {
	bigi := big.NewInt(0).SetBytes(a[i])
	bigj := big.NewInt(0).SetBytes(a[j])

	if bigi.Cmp(bigj) == -1 {
		return true
	}
	return false
}