Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "sort"
- import "fmt"
- type ByLength []string
- func (s ByLength) Len() int {
- return len(s)
- }
- func (s ByLength) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- func (s ByLength) Less(i, j int) bool {
- return len(s[i]) < len(s[j])
- }
- func main() {
- fruits := []string{"peach", "banana", "kiwi"}
- sort.Sort(ByLength(fruits))
- fmt.Println(fruits)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement