GO Language Basics-04-Data Type-04-map (sorting of maps)

1. Sort by value

1.1 Ideas

  • mapThe sequence itself is uncontrollable. We consider the following methods to implement queuing:
    • Idea 1: Use one 切片to receive mapthe members of the good team
    • Idea 2: Use a 切片receiving mapmember to 切片queue
  • We found sort.Slice()a way to easily queue slices, so decided to use idea two

1.2 Grammar

  • grammar
func Slice(x any, less func(i int, j int) bool)

The function in the parameter defines a comparison method

  • syntax example
	sort.Slice(mySlice, func(i, j int) bool {
    
    
		return mySlice[i].Weight < mySlice[j].Weight
	})

1.3 Complete example

package main

import (
	"fmt"
	"sort"
)

//创建一个切片准备接收map的数据
type Fruit struct {
    
    
	Name   string
	Weight int64
}

func main() {
    
    
    //实例化一个map
	fruitMap := map[string]int64{
    
    
		"banana": 7,
		"apple":  5,
		"orange": 6,
		"peach":  8,
		"lemon":  9,
	}

    //创建一个接收结构体的切片,将map的值传入切片
	var fruitList []Fruit
	for k, v := range fruitMap {
    
    
		fruitList = append(fruitList, Fruit{
    
    k, v})
	}
	fmt.Printf("排序前:%+v\n", fruitList)

    //给切片排序
	sort.Slice(fruitList, func(i, j int) bool {
    
    
		return fruitList[i].Weight < fruitList[j].Weight
	})
	fmt.Printf("排序后:%+v\n", fruitList)
}
  • output result
排序前:[{
    
    Name:peach Weight:8} {
    
    Name:lemon Weight:9} {
    
    Name:banana Weight:7} {
    
    Name:apple Weight:5} {
    
    Name:orange Weight:6}]
排序后:[{
    
    Name:apple Weight:5} {
    
    Name:orange Weight:6} {
    
    Name:banana Weight:7} {
    
    Name:peach Weight:8} {
    
    Name:lemon Weight:9}]

As can be seen above, the receiving slice fruitList is chaotic before sorting (because the order of map is uncontrollable), after sorting, it can be output according to size

2. Sort by key

2.1 Ideas

  • Use a slice to receive the key of the map
  • Sort slice members
  • Output map members by slice

2.2 Syntax example

func Sort(data Interface)

2.3 Complete example

  • the code
package main

import (
	"fmt"
	"sort"
)

func main() {
    
    
	//定义一个map
	MyMap := map[int]int64{
    
    
		3: 7,
		2: 5,
		4: 6,
		7: 8,
		6: 9,
	}
	//定义一个切片组,接收map的key
	keys := []int{
    
    }
	for key := range MyMap {
    
    
		keys = append(keys, key)
	}
	//给切片排序
	sort.Sort(sort.IntSlice(keys))
   //按着切片输出map(当然你也可以用另一个切片接收)
	for _, key := range keys {
    
    
		fmt.Printf("%v : %v\n", key, MyMap[key])
	}
}
  • output
2 : 5
3 : 7
4 : 6
6 : 9
7 : 8

2.4 Complete example

1.3Of course, it is also possible for us to directly use slices to receive and sort according to the method in

  • the code
package main

import (
	"fmt"
	"sort"
)

// 创建一个切片准备接收map的数据
type Fruit struct {
    
    
	Key   int
	Value int64
}

func main() {
    
    
	//实例化一个map
	fruitMap := map[int]int64{
    
    
		3: 7,
		2: 5,
		4: 6,
		7: 8,
		6: 9,
	}

	//创建一个接收结构体的切片,将map的值传入切片
	var fruitList []Fruit
	for k, v := range fruitMap {
    
    
		fruitList = append(fruitList, Fruit{
    
    k, v})
	}
	fmt.Printf("排序前:%+v\n", fruitList)

	//给切片排序
	sort.Slice(fruitList, func(i, j int) bool {
    
    
		return fruitList[i].Key < fruitList[j].Key
	})
	fmt.Printf("排序后:%+v\n", fruitList)
}
  • output
排序前:[{
    
    Key:3 Value:7} {
    
    Key:2 Value:5} {
    
    Key:4 Value:6} {
    
    Key:7 Value:8} {
    
    Key:6 Value:9}]
排序后:[{
    
    Key:2 Value:5} {
    
    Key:3 Value:7} {
    
    Key:4 Value:6} {
    
    Key:6 Value:9} {
    
    Key:7 Value:8}]


insert image description here

Guess you like

Origin blog.csdn.net/xingzuo_1840/article/details/131682199