1 回答
TA贡献1757条经验 获得超8个赞
为了扩展我对原始问题的评论,这里有一个可以在Go playground上运行的工作示例。请注意,我可能误解了您想要的类型的结构,但这个想法应该很清楚。
package main
import "fmt"
import "sort"
type Product struct {
Id string
Rating float64
}
type Deal struct {
Id string
products []Product
}
type Deals []Deal
// Ensure it satisfies sort.Interface
func (d Deals) Len() int { return len(d) }
func (d Deals) Less(i, j int) bool { return d[i].Id < d[j].Id }
func (d Deals) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func main() {
deals := Deals{
{"9970DLXEVOQ0O", []Product{{"9972MFYWYJIEK", 0.2}}},
{"9970DLXEVOQ01", []Product{{"9972IOFNIDER6", 0.3}}},
{"9970QYPOYUUIO", []Product{{"9972VOFA3OJLK", 0.4}}},
{"9970DLYKLAO8O", []Product{{"9972IOFNIDER6", 0.3}}},
{"9970QYPMNAUUI", []Product{{"9972QIUUINW6R", 0.5}}},
}
sort.Sort(deals)
for _, d := range deals {
fmt.Println(d)
}
}
- 1 回答
- 0 关注
- 246 浏览
添加回答
举报