Programming Code Center(PCC)
[GO]

(PCC)::[How-to-sort-strings-by-their-length-using-Sorting-by-Functions-in-go-language]::[go]

File Name : sorting-by-functions.go

package main

import (
    "fmt"
    "sort"
)

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

Output :

sorting-by-functions.jpg