Go实例: Sorting by Functions

Ursula ·
更新时间:2024-11-13
· 308 次阅读

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) }$ go run sorting-by-functions.go [kiwi peach banana]



GO实例 sorting GO BY

需要 登录 后方可回复, 如果你还没有账号请 注册新账号