Sorting a map[string][]struct{}

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

Sorting a map[string][]struct{}

问题

我会帮你翻译以下内容:

我想按照cost对这个地图进行排序

type Graph struct {
    vertice string
    cost    float64
}

var graph map[string][]Graph

按照从低到高的顺序排列

谢谢!

英文:

I would likde to sort this map by cost

type Graph struct {
    vertice string
    cost    float64
}

var graph map[string][]Graph

In the order of lowest to highest

Thanks!

答案1

得分: 1

如果目标是按照成本对graph中的每个切片进行排序,你只需要在[]Graph上实现sort.Interface,然后使用for循环遍历值。

type ByCost []Graph

func (gs *ByCost) Len() int { return len(gs) }
func (gs *ByCost) Less(i, j int) bool { return gs[i].cost < gs[j].cost }
func (gs *ByCost) Swap(i, j int) { gs[i], gs[j] = gs[j], gs[i] }

for _, v := range graph {
    sort.Sort(ByCost(v))
}

如果你想按照[]Graph中成本的总和按排序顺序迭代遍历映射,那将会更加复杂。

type GraphKeyPairs struct {
    key string
    value []Graph
}

// 构建一个切片来存储映射的值
sortedGraph := make([]GraphKeyPairs, 0, len(graph))
for k,v := range graph {
    // O(n)
    gkp := GraphKeyPairs{key: k, value: v}
    sortedGraph = append(sortedGraph, gkp)
}

type BySummedCost []GraphKeyPairs

func (gkp *BySummedCost) Len() int { return len(gkp) }
func (gkp *BySummedCost) Swap(i, j int) { gkp[i], gkp[j] = gkp[j], gkp[i] }

func (gkp *BySummedCost) Less(i, j int) bool {
    // O(2n)
    iCost, jCost := 0, 0
    for _, v := range gkp[i].value {
        iCost += v.cost
    }
    for _, v := range gkp[j].value {
        jCost += v.cost
    }
    return iCost < jCost
}

sort.Sort(BySummedCost(sortedGraph))
英文:

If the goal is to sort each slice in graph by cost, you only need to implement sort.Interface on []Graph, then use a for loop to loop through the values.

type ByCost []Graph

func (gs *ByCost) Len() int { return len(gs) }
func (gs *ByCost) Less(i, j int) bool { return gs[i].cost &lt; gs[j].cost }
func (gs *ByCost) Swap(i, j int) { gs[i], gs[j] = gs[j], gs[i] }

for _, v := range graph {
    sort.Sort(ByCost(v))

If you're trying to iterate through the map in sorted order by the sum of the costs in the []Graph, that's going to be much less clean.

type GraphKeyPairs struct {
    key string
    value []Graph
}

// Build a slice to store our map values
sortedGraph := make([]GraphKeyPairs, 0, len(graph))
for k,v := range graph {
    // O(n)
    gkp := GraphKeyPairs{key: k, value: v}
    sortedGraph = append(sortedGraph, gkp)
}

type BySummedCost []GraphKeyPairs

func (gkp *BySummedCost) Len() int { return len(gkp) }
func (gkp *BySummedCost) Swap(i, j int) { gkp[i], gkp[j] = gkp[j], gkp[i] }

func (gkp *BySummedCost) Less(i, j int) bool {
    // O(2n)
    iCost, jCost := 0, 0
    for _, v := range gkp[i].value {
        iCost += v.cost
    }
    for _, v := range gkp[j].value {
        jCost += v.cost
    }
    return iCost &lt; jCost
}

sort.Sort(BySummedCost(sortedGraph))

huangapple
  • 本文由 发表于 2016年1月23日 08:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/34958036.html
匿名

发表评论

匿名网友

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

确定