Sorting IP:Ports in Golang
by Vikram Melkote
Setup
-
git clone into ~/go/src
or download .go
files and place in ~/go/src/ipsorting
:
git clone https://github.com/Vikrammel/golang-ipsorting.git
-
if vendoring, place .go
files in <projectroot>/app/vendor/ipsorting
-
import package in code:
import (
"ipsorting"
)
Usage in code
use on string or an array/slice of strings of ip:ports (ipsorting.go can be easily modified to work with just IPs instead of IP:Port)
-
compare two IP:Ports
sortedPair := ipsorting.OrderIPPair("10.0.0.14:8080", "10.0.0.14:5060")
log.Println(sortedPair[0])
-
sort a slice of IP:Ports
sortedList := ipsorting.SortIPs(unsortedList)
log.Println(sortedList)
-
insert IP:Port into slice in correct position
sortedList = ipsorting.InsortIP(sortedList, "10.0.0.23:8070")
log.Println(sortedList)
Reference
Quicksort in python
Binary Search [for InsortIP()]
Feel free to use this in any of your projects and please let me know if you encounter any bugs. Thank you.