英文:
sort a map of structs in golang
问题
我有一个类似这样的地图:
Key: 9970DLXEVOQ0O Value: [{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2},{9972QIUUINW6R 0.5}]
Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]
在Golang中,它被命名为product_deal,其中key是字符串,value是一个结构体:
type product_detail struct {
deal_id string
rating float64
}
我需要根据每个值字段中的评分(降序)对值进行排序,输出应该是:
Key: 9970DLXEVOQ0O Value: [{9972QIUUINW6R 0.5},{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2}]
Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]
有什么想法如何实现这个?我看了其他帖子关于如何对地图进行排序,但是没有找到实现的方法。任何帮助将不胜感激。
英文:
I have a map which is like this
Key: 9970DLXEVOQ0O Value: [{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2},{9972QIUUINW6R 0.5}]
Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]
in golang named product_deal in which key is string and value is a struct :
type product_detail struct {
deal_id string
rating float64
}
I need to sort the values based on ratings(descending ) in each value field the output should be i.e
Key: 9970DLXEVOQ0O Value: [{9972QIUUINW6R 0.5},{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2}]
Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]
Any ideas of how this needs to be done. i did have a look at other post which sort maps but could not get the implementation.
Any help would be appreciated.
答案1
得分: 12
以下是原始问题的评论的详细说明,这是一个可以在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
// 确保它满足 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)
}
}
希望对您有所帮助!
英文:
To expand on my comment on the original question, here is a working example which can be run on the Go playground. Note that I may have misinterpreted the structure of the types you want to have, but the idea should be clear.
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论