ipsort/main.go

40 lines
634 B
Go

// command svsort sorts lines of text by semver at beginning of line
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"net"
"os"
"sort"
)
type lines []string
func (l lines) Len() int { return len(l) }
func (l lines) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l lines) Less(i, j int) bool {
a, b := net.ParseIP(l[i]), net.ParseIP(l[j])
return bytes.Compare(a, b) < 0
}
func main() {
s := bufio.NewScanner(os.Stdin)
ls := lines{}
for s.Scan() {
ls = append(ls, s.Text())
}
if err := s.Err(); err != nil {
log.Fatalf("scan: %v")
}
sort.Sort(ls)
for _, l := range ls {
fmt.Printf("%v\n", l)
}
}