简体   繁体   中英

How to sort a map[string]float64 by the float64 value?

I'm struggling to understand how to simply sort a map[string]float64 . I had a look at How to sort a Map[string]int by its values? , which suggests using a struct to do the sort, but I'm not sure how to do this when the sort.Sort func expects func(i, j int) and not func(i, j float64) .

For example, how would this be sorted?

data := make(map[string]float64)
data["red"] = 1.00
data["green"] = 3.00
data["blue"] = 2.00

I tried the following, but that only sorts by the string in the map, not the actual float64 :

data := make(map[string]float64)
data["red"] = 1.00
data["green"] = 3.00
data["blue"] = 2.00

var keys []string
var values []float64
for key, cost := range data {
    keys = append(keys, key)
    costs = append(costs, cost)
}
sort.Strings(keys)
sort.Float64s(values)
for _, key := range keys {
    fmt.Printf("%s %v\n", key, data[key])
}

It expects int instead of float64 because i, j are indices to use on comparing and swapping elements in a slice. I recommend you should use a new struct and implement the sort.Interface for it:

type MapData struct {
    Key   string
    Value float64
}

type MapSort []*MapData

func (m MapSort) Len() int {
    return len(m)
}
func (m MapSort) Less(i, j int) bool {
    return m[i].Value < m[j].Value
}
func (m MapSort) Swap(i, j int) {
    m[i], m[j] = m[j], m[i]
}

Thanks to Marc for pointing out my mistake, here's what I'm using to sort successfully:

package main

import (
    "fmt"
    "math"
    "sort"
)

// round a float64 to 2 decimal places.
func round(n float64) float64 {
    return math.Round(n*100) / 100
}

type Pair struct {
    Key   string
    Value float64
}

type PairList []Pair

func (p PairList) Len() int           { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PairList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func main() {
    data := make(map[string]float64)
    data["red"] = 1.00
    data["green"] = 3.00
    data["blue"] = 2.00

    var i int
    sorted := make(PairList, len(data))
    for key, value := range data {
        sorted[i] = Pair{key, value}
        i++
    }
    sort.Sort(sort.Reverse(sorted))
    for _, pair := range sorted {
        fmt.Printf("%s %v\n", pair.Key, round(pair.Value))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM