sort ip addresses in numerical order
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
634 B

// 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)
}
}