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

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

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 &quot;fmt&quot;
import &quot;sort&quot;

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 &lt; d[j].Id }
func (d Deals) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	deals := Deals{
		{&quot;9970DLXEVOQ0O&quot;, []Product{{&quot;9972MFYWYJIEK&quot;, 0.2}}},
		{&quot;9970DLXEVOQ01&quot;, []Product{{&quot;9972IOFNIDER6&quot;, 0.3}}},
		{&quot;9970QYPOYUUIO&quot;, []Product{{&quot;9972VOFA3OJLK&quot;, 0.4}}},
		{&quot;9970DLYKLAO8O&quot;, []Product{{&quot;9972IOFNIDER6&quot;, 0.3}}},
		{&quot;9970QYPMNAUUI&quot;, []Product{{&quot;9972QIUUINW6R&quot;, 0.5}}},
	}

	sort.Sort(deals)

	for _, d := range deals {
		fmt.Println(d)
	}
}

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:

确定