Go
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func main() {
// 创建一个一维结构体切片
people := []Person{
{"Alice", 25},
{"Bob", 30},
{"Charlie", 20},
{"David", 35},
{"Eve", 28},
{"Frank", 32},
}
// 根据 Age 字段对一维结构体切片进行倒序排序
sort.Slice(people, func(i, j int) bool {
return people[i].Age > people[j].Age
})
// 打印排序后的一维结构体切片
for _, person := range people {
fmt.Printf("Name: %s, Age: %d\n", person.Name, person.Age)
}
}
类似php的array_multisort函数