github.com/auxten/ginkgo@v0.0.0-20220130172820-7d98ad59d232/seed/dedup.go (about)

     1  package seed
     2  
     3  import (
     4  	"bytes"
     5  	"sort"
     6  )
     7  
     8  func SortDeDup(l []Host) []Host {
     9  	n := len(l)
    10  	if n <= 1 {
    11  		return l
    12  	}
    13  	sort.Slice(l, func(i, j int) bool {
    14  		c := bytes.Compare(l[i].IP[:], l[j].IP[:])
    15  		if c != 0 {
    16  			return c < 0
    17  		} else {
    18  			return l[i].Port < l[j].Port
    19  		}
    20  	})
    21  
    22  	j := 1
    23  	for i := 1; i < n; i++ {
    24  		if l[i] != l[i-1] {
    25  			l[j] = l[i]
    26  			j++
    27  		}
    28  	}
    29  
    30  	return l[0:j]
    31  }