英文:
How can I sort a Map[string]int by its values?
问题
给定以下代码块:
map[string]int{"hello":10, "foo":20, "bar":20}
我想要按照从高到低的顺序打印出:
foo, 20
bar, 20
hello, 10
英文:
Given this code block
map[string]int {"hello":10, "foo":20, "bar":20}
I would like to print out
foo, 20
bar, 20
hello, 10
In the order of highest to lowest
答案1
得分: 110
在Golang-nuts上找到了Andrew Gerrand的答案。
你可以通过编写len/less/swap函数来实现排序接口。
func rankByWordCount(wordFrequencies map[string]int) PairList{
pl := make(PairList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
type Pair struct {
Key string
Value int
}
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] }
有关原始帖子,请在此处找到:https://groups.google.com/forum/#!topic/golang-nuts/FT7cjmcL7gw
英文:
Found the answer on Golang-nuts by Andrew Gerrand
You can implement the sort interface by writing the len/less/swap functions
func rankByWordCount(wordFrequencies map[string]int) PairList{
pl := make(PairList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
type Pair struct {
Key string
Value int
}
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] }
For the original post, please find it here https://groups.google.com/forum/#!topic/golang-nuts/FT7cjmcL7gw
答案2
得分: 104
在Go 1.8中引入了一个新的sort.Slice函数,所以现在这个代码更简单了。
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{
"something": 10,
"yo": 20,
"blah": 20,
}
type kv struct {
Key string
Value int
}
var ss []kv
for k, v := range m {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
for _, kv := range ss {
fmt.Printf("%s, %d\n", kv.Key, kv.Value)
}
}
链接:https://play.golang.org/p/y1_WBENH4N
英文:
There's a new sort.Slice function in go 1.8, so now this is simpler.
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{
"something": 10,
"yo": 20,
"blah": 20,
}
type kv struct {
Key string
Value int
}
var ss []kv
for k, v := range m {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
for _, kv := range ss {
fmt.Printf("%s, %d\n", kv.Key, kv.Value)
}
}
答案3
得分: 21
首先按值对键进行排序,然后迭代映射:
package main
import (
"fmt"
"sort"
)
func main() {
counts := map[string]int{"hello": 10, "foo": 20, "bar": 20}
keys := make([]string, 0, len(counts))
for key := range counts {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool { return counts[keys[i]] > counts[keys[j]] })
for _, key := range keys {
fmt.Printf("%s, %d\n", key, counts[key])
}
}
请注意,这是一个Go语言的示例代码,用于按值对键进行排序并迭代映射。
英文:
Sort keys first by value and then iterate map:
package main
import (
"fmt"
"sort"
)
func main() {
counts := map[string]int{"hello": 10, "foo": 20, "bar": 20}
keys := make([]string, 0, len(counts))
for key := range counts {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool { return counts[keys[i]] > counts[keys[j]] })
for _, key := range keys {
fmt.Printf("%s, %d\n", key, counts[key])
}
}
答案4
得分: 19
例如:
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{"hello": 10, "foo": 20, "bar": 20}
n := map[int][]string{}
var a []int
for k, v := range m {
n[v] = append(n[v], k)
}
for k := range n {
a = append(a, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
for _, k := range a {
for _, s := range n[k] {
fmt.Printf("%s, %d\n", s, k)
}
}
}
输出:
foo, 20
bar, 20
hello, 10
英文:
For example:
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{"hello": 10, "foo": 20, "bar": 20}
n := map[int][]string{}
var a []int
for k, v := range m {
n[v] = append(n[v], k)
}
for k := range n {
a = append(a, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
for _, k := range a {
for _, s := range n[k] {
fmt.Printf("%s, %d\n", s, k)
}
}
}
Output:
foo, 20
bar, 20
hello, 10
答案5
得分: 3
我经常需要对一个map[string]int
进行排序,该map用于计数,我一直在使用以下代码:
func rankMapStringInt(values map[string]int) []string {
type kv struct {
Key string
Value int
}
var ss []kv
for k, v := range values {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
ranked := make([]string, len(values))
for i, kv := range ss {
ranked[i] = kv.Key
}
return ranked
}
使用它按值的顺序迭代键:
values := map[string]int{"foo": 10, "bar": 20, "baz": 1}
for i, index := range rankMapStringInt(values) {
fmt.Printf("%3d: %s -> %d", i, index, values[index])
}
英文:
I often need to sort a map[string]int
of something I’m counting and have been using the following.
func rankMapStringInt(values map[string]int) []string {
type kv struct {
Key string
Value int
}
var ss []kv
for k, v := range values {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
ranked := make([]string, len(values))
for i, kv := range ss {
ranked[i] = kv.Key
}
return ranked
}
Use it to iterate over the keys in order of value
values := map[string]int{"foo": 10, "bar": 20, "baz": 1}
for i, index := range rankMapStringInt(values) {
fmt.Printf("%3d: %s -> %d", i, index, values[index])
}
答案6
得分: 1
在我的情况下,我正在处理一个我创建的程序。在这个程序中,我创建了一个和你一样的Map,其中包含string
和int
。然后我发现,就像你一样,Go语言实际上没有内置的方法来对这样的数据进行排序。我阅读了其他答案,但并不是很满意。
所以我尝试以不同的方式思考这个问题。Go语言可以使用sort.Ints
来对切片进行排序。此外,Go语言还可以使用自定义比较器的sort.Slice
。因此,我不再创建string
和int
的Map,而是创建了一个包含string
和int
的struct
。然后你可以进行排序:
package main
import (
"fmt"
"sort"
)
type file struct {
name string
size int
}
func main() {
a := []file{
{"april.txt", 9}, {"may.txt", 7},
}
sort.Slice(a, func (d, e int) bool {
return a[d].size < a[e].size
})
fmt.Println(a)
}
这种方法可能并不适用于所有人,因为你可能被迫处理其他人创建的Map。但对我来说很有用。好处是,与其他所有答案不同,这个方法不使用循环。
英文:
In my case, I was dealing with a program that I created. In this program, I created a Map just like you, with string
and int
. Then I discovered like you that Go doesn't really have a built-in way to sort something like this. I read the other answers and didn't really like what I read.
So I tried to think about the problem differently. Go can use sort.Ints with a slice. Also, Go can use sort.Slice with a custom comparator. So instead of creating a Map of string
and int
, I created a struct
of string
and int
. Then you can sort:
package main
import (
"fmt"
"sort"
)
type file struct {
name string
size int
}
func main() {
a := []file{
{"april.txt", 9}, {"may.txt", 7},
}
sort.Slice(a, func (d, e int) bool {
return a[d].size < a[e].size
})
fmt.Println(a)
}
This will not work for everyone, because maybe you will be forced to deal with a map someone else created. But it was useful for me. Good part is, unlike all the other answers, this one uses no loops.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论