如何使用GoLang对自定义 struct 数组进行排序.
我的代码是:
package main
import "fmt"
type TicketDistribution struct {
Label string
TicketVolume int64
}
type TicketDistributionResponse struct {
LevelDistribution []*TicketDistribution
}
func main() {
var response TicketDistributionResponse
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "John", TicketVolume: 3})
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "Bill", TicketVolume: 7})
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "Sam", TicketVolume: 4})
for _, val := range response.LevelDistribution {
fmt.Println(*val)
}
}
这会将输出打印为
{John 3}
{Bill 7}
{Sam 4}
我想按TicketVolume值按降序对response对象进行排序.
在排序之后,响应对象应该如下所示:
{Bill 7}
{Sam 4}
{John 3}