当前位置:网站首页>Go language map sorting (key/value sorting)

Go language map sorting (key/value sorting)

2022-06-25 05:44:00 potato

1、 Preface

Go Linguistic map Is chaotic , Go through it many times map The results may be different , such as :

package main

import (
    "fmt"
)

func Map() (result map[int]uint32) {
    result = map[int]uint32{}
    //  Insert individual data 
    result[24] = 240
    result[17] = 170
    result[9] = 90
    result[11] = 110
    result[55] = 550
    return
}
func main() {
    mapResult := Map()
    //  Traverse map
    for key, value := range mapResult {
        fmt.Printf("key = %v,value = %v\n", key, value)
    }
}

The first traversal result is as follows :
 Insert picture description here
The result of the second traversal is as follows :
 Insert picture description here
You can see that the results of the two iterations are different

2、 Realization map Ergodic order

2.1 key Orderly

Ideas : Yes key Sort , Re traversal key Output value

2.1.1 Sort from small to large

sort.Sort(sort.IntSlice(keys))

The code is as follows :

package main

import (
    "fmt"
    "sort"
)

func Map() (result map[int]uint32, keys []int) {
    result = map[int]uint32{}
    keys = []int{}
    //  Insert individual data 
    result[24] = 240
    result[17] = 170
    result[9] = 90
    result[11] = 110
    result[55] = 550
    //  Get each one key
    for key := range result {
        keys = append(keys, key)
    }
    //  to key Sort , From small to large 
    sort.Sort(sort.IntSlice(keys))
    return
}
func main() {
    mapResult, keys := Map()
    //  Traverse map
    for _, key := range keys {
        fmt.Printf("key = %v,value = %v\n", key, mapResult[key])
    }
}

result :
 Insert picture description here

2.1.2 Sort from large to small

sort.Sort(sort.Reverse(sort.IntSlice(keys)))

The code is as follows :

package main

import (
    "fmt"
    "sort"
)

func Map() (result map[int]uint32, keys []int) {
    result = map[int]uint32{}
    keys = []int{}
    //  Insert individual data 
    result[24] = 240
    result[17] = 170
    result[9] = 90
    result[11] = 110
    result[55] = 550
    //  Get each one key
    for key := range result {
        keys = append(keys, key)
    }
    //  to key Sort , From big to small 
    sort.Sort(sort.Reverse(sort.IntSlice(keys)))
    return
}
func main() {
    mapResult, keys := Map()
    //  Traverse map
    for _, key := range keys {
        fmt.Printf("key = %v,value = %v\n", key, mapResult[key])
    }
}

result :
 Insert picture description here

2.2 value Orderly

use struct Deposit key and value, Realization sort Interface , You can call sort.Sort It's sorted

2.2.1 Sort from small to large

package main

import (
    "fmt"
    "sort"
)

func main() {
    mapInfo := map[string]int32{
        "roy":18,
        "kitty":16,
        "hugo":21,
        "tina":35,
        "jason":23,
    }

    type peroson struct {
        Name string
        Age int32
    }

    var lstPerson []peroson
    for k, v := range mapInfo {
        lstPerson = append(lstPerson, peroson {k, v})
    }

    sort.Slice(lstPerson, func(i, j int) bool {
        return lstPerson[i].Age < lstPerson[j].Age  //  Ascending 
    })
    fmt.Println(lstPerson)
}

result :
 Insert picture description here

2.2.2 Sort from large to small

package main

import (
    "fmt"
    "sort"
)

func main() {
    mapInfo := map[string]int32{
        "roy":   18,
        "kitty": 16,
        "hugo":  21,
        "tina":  35,
        "jason": 23,
    }

    type peroson struct {
        Name string
        Age  int32
    }

    var lstPerson []peroson
    for k, v := range mapInfo {
        lstPerson = append(lstPerson, peroson{k, v})
    }

    sort.Slice(lstPerson, func(i, j int) bool {
        return lstPerson[i].Age > lstPerson[j].Age //  Descending 
    })
    fmt.Println(lstPerson)
}

result :
 Insert picture description here

原网站

版权声明
本文为[potato]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202202133505717.html