Golang排序:没有实现sort.Interface(缺少Len方法)

mar*_*ver 2 sorting go

我在Go应用程序中实现Sort接口时遇到问题.这是相关代码:

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]
            }
Run Code Online (Sandbox Code Playgroud)

所以我想根据他们的观点对团队的团队进行排序.但无论何时我跑

sort.Sort(group.Teams)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

 cannot use group.Teams (type []*Team) as type sort.Interface in argument
to sort.Sort:   []*Team does not implement sort.Interface (missing Len
method)
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

Tim*_*per 6

[]*Team是不一样的Teams; 你需要明确地使用或转换为后者:

type Group struct {
   Teams Teams
}
sort.Sort(group.Teams)
Run Code Online (Sandbox Code Playgroud)

要么:

type Group struct {
   Teams []*Team
}
sort.Sort(Teams(group.Teams))
Run Code Online (Sandbox Code Playgroud)