我在 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 argument
to sort.Sort: []*Team does not implement sort.Interface (missing Len
method)
为什么是这样?
三国纷争
相关分类