package mainimport ("fmt""sort"
)type Person struct {Name stringAge 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函数