GolangNote

Golang笔记

golang map 按value 大小排序,降序和升序

Permalink

map 按value 大小排序,降序和升序。

golang map 按value 大小排序,降序和升序

Go: go map 排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import "fmt"
import "sort"

func main() {
  m := map[string]int{
    "One":   100,
    "Two":   2,
    "Three": 3,
    "Ten":   10,
    "Fifty": 50,
  }
  vs := NewValSorter(m)
  fmt.Printf("%v\n", *vs)
  vs.Sort()
  fmt.Printf("%v\n", *vs)
}

type ValSorter struct {
  Keys []string
  Vals []int
}

func NewValSorter(m map[string]int) *ValSorter {
  vs := &ValSorter{
    Keys: make([]string, 0, len(m)),
    Vals: make([]int, 0, len(m)),
  }
  for k, v := range m {
    vs.Keys = append(vs.Keys, k)
    vs.Vals = append(vs.Vals, v)
  }
  return vs
}

func (vs *ValSorter) Sort() {
  sort.Sort(vs)
}

func (vs *ValSorter) Len() int           { return len(vs.Vals) }
func (vs *ValSorter) Less(i, j int) bool { return vs.Vals[i] < vs.Vals[j] }
func (vs *ValSorter) Swap(i, j int) {
  vs.Vals[i], vs.Vals[j] = vs.Vals[j], vs.Vals[i]
  vs.Keys[i], vs.Keys[j] = vs.Keys[j], vs.Keys[i]
}

输出:

Bash: 排序结果
1
2
{[One Two Three Ten Fifty] [100 2 3 10 50]}
{[Two Three Ten Fifty One] [2 3 10 50 100]}

这是按升序排序,如果要按降序排列则修改下面一行:

Go: Less
1
func (vs *ValSorter) Less(i, j int) bool { return vs.Vals[i] > vs.Vals[j] }

下面另一种实现简单一些:

Go: 排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

// sort a map's keys in descending order of its values.

import "sort"
import "fmt"

type sortedMap struct {
  m map[string]int
  s []string
}

func (sm *sortedMap) Len() int {
  return len(sm.m)
}

func (sm *sortedMap) Less(i, j int) bool {
  return sm.m[sm.s[i]] > sm.m[sm.s[j]]
}

func (sm *sortedMap) Swap(i, j int) {
  sm.s[i], sm.s[j] = sm.s[j], sm.s[i]
}

func sortedKeys(m map[string]int) []string {
  sm := new(sortedMap)
  sm.m = m
  sm.s = make([]string, len(m))
  i := 0
  for key, _ := range m {
    sm.s[i] = key
    i++
  }
  sort.Sort(sm)
  return sm.s
}

func main() {
  s := []string{"Python", "Python", "Python", "igor", "igor", "igor", "igor", "go", "go", "Golang", "Golang", "Golang", "Golang", "Py", "Py"}
  count := make(map[string]int)

  for _, v := range s {
    count[v]++
  }

  for _, res := range sortedKeys(count) {
    fmt.Println(res, count[res])
  }

}

go1.8 后提供的 slice sort 功能使排序更简单:

Go: slice sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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  // 降序
        // return ss[i].Value < ss[j].Value  // 升序
    })

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

输出:

Bash: 排序结果
1
2
3
yo, 20
blah, 20
something, 10

Related articles

Golang telegram 机器人小试

telegram 的机器人接口很开放,使用简单,100%开放无限制,相对微信服务号、公众号好很多。用来做一些小应用也很方便。下面是使用golang sdk 开发telegram 机器人的经验。...

golang Selenium WebDriver 使用记录

Selenium WebDriver 直接通过浏览器自动化的本地接口来调用浏览器,以达到模拟浏览器行为的操作,如点击、选择、鼠标移动等。下面是记录个人使用golang 驱动的记录。...

Golang 时区时差处理方式

个人习惯用 0 时区时间戳记录时间,可以方便转到不同时区,下面介绍 Golang 时区时差处理...

Write a Comment to "golang map 按value 大小排序,降序和升序"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 1ms