我在 Go 应用程序中实现排序接口时遇到问题。这是相关代码:type Group struct { Teams []*Team}type Team struct { Points int } type Teams []*Team func (slice Teams) Len() int { return len(slice) } func (slice Teams) Less(i, j int) bool { return slice[i].Points < slice[j].Points } func (slice Teams) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] }所以我想根据他们的分数对小组的团队进行排序。但每当我跑步sort.Sort(group.Teams)我收到此错误: cannot use group.Teams (type []*Team) as type sort.Interface in argumentto sort.Sort: []*Team does not implement sort.Interface (missing Lenmethod)为什么是这样?
1 回答
三国纷争
TA贡献1804条经验 获得超7个赞
[]*Team不一样Teams; 您需要明确使用或强制转换为后者:
type Group struct {
Teams Teams
}
sort.Sort(group.Teams)
要么:
type Group struct {
Teams []*Team
}
sort.Sort(Teams(group.Teams))
- 1 回答
- 0 关注
- 433 浏览
添加回答
举报
0/150
提交
取消