How can I sort a Map[string]int by its values?

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

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 &lt; 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 (
    &quot;fmt&quot;
    &quot;sort&quot;
)

func main() {
    m := map[string]int{
	    &quot;something&quot;: 10,
	    &quot;yo&quot;:        20,
	    &quot;blah&quot;:      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 &gt; ss[j].Value
	})

    for _, kv := range ss {
	    fmt.Printf(&quot;%s, %d\n&quot;, kv.Key, kv.Value)
    }
}

https://play.golang.org/p/y1_WBENH4N

答案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 (
    &quot;fmt&quot;
    &quot;sort&quot;
)

func main() {
    counts := map[string]int{&quot;hello&quot;: 10, &quot;foo&quot;: 20, &quot;bar&quot;: 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]] &gt; counts[keys[j]] })

    for _, key := range keys {
        fmt.Printf(&quot;%s, %d\n&quot;, 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)
                }
        }
}

Playground


输出:

foo, 20
bar, 20
hello, 10
英文:

For example:

package main

import (
        &quot;fmt&quot;
        &quot;sort&quot;
)

func main() {
        m := map[string]int{&quot;hello&quot;: 10, &quot;foo&quot;: 20, &quot;bar&quot;: 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(&quot;%s, %d\n&quot;, s, k)
                }
        }
}

Playground


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 &gt; 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{&quot;foo&quot;: 10, &quot;bar&quot;: 20, &quot;baz&quot;: 1}

for i, index := range rankMapStringInt(values) {
    fmt.Printf(&quot;%3d: %s -&gt; %d&quot;, i, index, values[index])
}

答案6

得分: 1

在我的情况下,我正在处理一个我创建的程序。在这个程序中,我创建了一个和你一样的Map,其中包含stringint。然后我发现,就像你一样,Go语言实际上没有内置的方法来对这样的数据进行排序。我阅读了其他答案,但并不是很满意。

所以我尝试以不同的方式思考这个问题。Go语言可以使用sort.Ints来对切片进行排序。此外,Go语言还可以使用自定义比较器的sort.Slice。因此,我不再创建stringint的Map,而是创建了一个包含stringintstruct。然后你可以进行排序:

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

type file struct {
   name string
   size int
}

func main() {
   a := []file{
      {&quot;april.txt&quot;, 9}, {&quot;may.txt&quot;, 7},
   }
   sort.Slice(a, func (d, e int) bool {
      return a[d].size &lt; 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.

huangapple
  • 本文由 发表于 2013年9月9日 17:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/18695346.html
匿名

发表评论

匿名网友

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

确定