Skip to content

Commit

Permalink
pkg: Use sort.Slice (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku authored Mar 28, 2019
1 parent a88a371 commit 96a0afc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
16 changes: 1 addition & 15 deletions pkg/types/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewURLs(strs []string) (URLs, error) {
all[i] = *u
}
us := URLs(all)
us.Sort()
sort.Slice(us, func(i, j int) bool { return us[i].String() < us[j].String() })

return us, nil
}
Expand All @@ -46,20 +46,6 @@ func (us URLs) String() string {
return strings.Join(us.StringSlice(), ",")
}

// Sort sorts the URLs
func (us *URLs) Sort() {
sort.Sort(us)
}

// Len return the lenght of URL slice
func (us URLs) Len() int { return len(us) }

// Less compares two URL and return the less one
func (us URLs) Less(i, j int) bool { return us[i].String() < us[j].String() }

// Swap swaps two URLs in the slice
func (us URLs) Swap(i, j int) { us[i], us[j] = us[j], us[i] }

// StringSlice return a slice of formatted string of URL
func (us URLs) StringSlice() []string {
out := make([]string, len(us))
Expand Down
7 changes: 6 additions & 1 deletion pkg/types/urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ type testTypesSuite struct{}

func (s *testTypesSuite) TestURLs(c *C) {
urlstrs := []string{
"http://www.google.com:12306",
"http://192.168.199.111:1080",
"http://hostname:9000",
}
sorted := []string{
"http://192.168.199.111:1080",
"http://hostname:9000",
"http://www.google.com:12306",
}

urls, err := NewURLs(urlstrs)
c.Assert(err, IsNil)
c.Assert(urls.String(), Equals, strings.Join(urlstrs, ","))
c.Assert(urls.String(), Equals, strings.Join(sorted, ","))
}

func (s *testTypesSuite) TestBadURLs(c *C) {
Expand Down

0 comments on commit 96a0afc

Please sign in to comment.