やったこと
Goでsortパッケージを使ってみます。
調査
package main
import (
"fmt"
"runtime"
"sort"
)
func main() {
fmt.Println("確認環境: ", runtime.Version())
people := []struct {
Name string
Age int
}{
{"Gopher", 7},
{"Alice", 55},
{"Vera", 24},
{"Bob", 75},
}
sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
fmt.Println("By age 昇順:", people)
sort.Slice(people, func(i, j int) bool { return people[i].Age > people[j].Age })
fmt.Println("By age 降順:", people)
p := []int {7, 55, 24, 75}
sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })
fmt.Println(p)
}
出力結果
確認環境: go1.15.6
By age 昇順: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}]
By age 降順: [{Bob 75} {Alice 55} {Vera 24} {Gopher 7}]
[7 24 55 75]