1 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊
為了擴(kuò)展我對原始問題的評論,這里有一個(gè)可以在Go playground上運(yùn)行的工作示例。請注意,我可能誤解了您想要的類型的結(jié)構(gòu),但這個(gè)想法應(yīng)該很清楚。
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 關(guān)注
- 266 瀏覽
添加回答
舉報(bào)