在Go语言中对结构体的映射进行排序。

huangapple go评论123阅读模式
英文:

sort a map of structs in golang

问题

我有一个类似这样的地图:

  1. Key: 9970DLXEVOQ0O Value: [{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2},{9972QIUUINW6R 0.5}]
  2. Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
  3. Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]

在Golang中,它被命名为product_deal,其中key是字符串,value是一个结构体:

  1. type product_detail struct {
  2. deal_id string
  3. rating float64
  4. }

我需要根据每个值字段中的评分(降序)对值进行排序,输出应该是:

  1. Key: 9970DLXEVOQ0O Value: [{9972QIUUINW6R 0.5},{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2}]
  2. Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
  3. Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]

有什么想法如何实现这个?我看了其他帖子关于如何对地图进行排序,但是没有找到实现的方法。任何帮助将不胜感激。

英文:

I have a map which is like this

  1. Key: 9970DLXEVOQ0O Value: [{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2},{9972QIUUINW6R 0.5}]
  2. Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
  3. Key: 9970QYPOYUUIO Value: [{9972VOFA3OJLK 0.4}]

in golang named product_deal in which key is string and value is a struct :

  1. type product_detail struct {
  2. deal_id string
  3. rating float64
  4. }

I need to sort the values based on ratings(descending ) in each value field the output should be i.e

  1. Key: 9970DLXEVOQ0O Value: [{9972QIUUINW6R 0.5},{9972IOFNIDER6 0.3},{9972MFYWYJIEK 0.2}]
  2. Key: 9970DLXEVOQ01 Value: [{9972IOFNIDER6 0.3}]
  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上运行的示例。请注意,我可能对您想要的类型结构进行了错误解释,但是思路应该是清楚的。

  1. package main
  2. import "fmt"
  3. import "sort"
  4. type Product struct {
  5. Id string
  6. Rating float64
  7. }
  8. type Deal struct {
  9. Id string
  10. products []Product
  11. }
  12. type Deals []Deal
  13. // 确保它满足 sort.Interface 接口
  14. func (d Deals) Len() int { return len(d) }
  15. func (d Deals) Less(i, j int) bool { return d[i].Id < d[j].Id }
  16. func (d Deals) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
  17. func main() {
  18. deals := Deals{
  19. {"9970DLXEVOQ0O", []Product{{"9972MFYWYJIEK", 0.2}}},
  20. {"9970DLXEVOQ01", []Product{{"9972IOFNIDER6", 0.3}}},
  21. {"9970QYPOYUUIO", []Product{{"9972VOFA3OJLK", 0.4}}},
  22. {"9970DLYKLAO8O", []Product{{"9972IOFNIDER6", 0.3}}},
  23. {"9970QYPMNAUUI", []Product{{"9972QIUUINW6R", 0.5}}},
  24. }
  25. sort.Sort(deals)
  26. for _, d := range deals {
  27. fmt.Println(d)
  28. }
  29. }

希望对您有所帮助!

英文:

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.

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;sort&quot;
  4. type Product struct {
  5. Id string
  6. Rating float64
  7. }
  8. type Deal struct {
  9. Id string
  10. products []Product
  11. }
  12. type Deals []Deal
  13. // Ensure it satisfies sort.Interface
  14. func (d Deals) Len() int { return len(d) }
  15. func (d Deals) Less(i, j int) bool { return d[i].Id &lt; d[j].Id }
  16. func (d Deals) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
  17. func main() {
  18. deals := Deals{
  19. {&quot;9970DLXEVOQ0O&quot;, []Product{{&quot;9972MFYWYJIEK&quot;, 0.2}}},
  20. {&quot;9970DLXEVOQ01&quot;, []Product{{&quot;9972IOFNIDER6&quot;, 0.3}}},
  21. {&quot;9970QYPOYUUIO&quot;, []Product{{&quot;9972VOFA3OJLK&quot;, 0.4}}},
  22. {&quot;9970DLYKLAO8O&quot;, []Product{{&quot;9972IOFNIDER6&quot;, 0.3}}},
  23. {&quot;9970QYPMNAUUI&quot;, []Product{{&quot;9972QIUUINW6R&quot;, 0.5}}},
  24. }
  25. sort.Sort(deals)
  26. for _, d := range deals {
  27. fmt.Println(d)
  28. }
  29. }

huangapple
  • 本文由 发表于 2014年3月13日 09:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/22367201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定