Implementation of random sorting of golang array

Time:2022-6-2
catalogue
  • preface
  • The specific implementation steps are as follows
    • 1. import and storage
    • 2. assemble data and sort (scheme I)
    • 3. assemble data and sort (scheme II)
  • summary

preface

At present, we have received a demand for recommended data. We need to randomly sort the data obtained in the database and return it to the user. After considering, there are two ways to use it. One is through the database order by Rand (), and the other is the code processing required in this article

The specific implementation steps are as follows

1. import and storage

The codes are as follows:

?
1
2
3
4
5
import (
 "fmt"
 "math/rand"
 "time"
)

2. assemble data and sort (scheme I)

The codes are as follows:

?
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
type CategoryEntity struct {
 Grouponid Int64 // group ID
 Merchandiseid Int64 // product ID
 Categoryid Int64 // category ID
 Categorytitle string // category name
}
 
func main() {
 data := make([]CategoryEntity, 10)
 Data[0] = categoryentity{grouponid: 0, merchandiseid: 1117891, categoryid: 726, categorytitle: "vegetable"}
 Data[1] = categoryentity{grouponid: 1, merchandiseid: 1110162, categoryid: 1505, categorytitle: "seasoning"}
 Data[2] = categoryentity{grouponid: 2, merchandiseid: 1117822, categoryid: 746, categorytitle: "fruit"}
 Data[3] = categoryentity{grouponid: 3, merchandiseid: 1115770, categoryid: 1408, categorytitle: "personal care"}
 Data[4] = categoryentity{grouponid: 4, merchandiseid: 1116528, categoryid: 732, categorytitle: "meat"}
 Data[5] = categoryentity{grouponid: 5, merchandiseid: 1116526, categoryid: 727, categorytitle: "snack food"}
 Data[6] = categoryentity{grouponid: 6, merchandiseid: 1117188, categoryid: 728, categorytitle: "grain and oil seasoning"}
 Data[7] = categoryentity{grouponid: 7, merchandiseid: 1117379, categoryid: 726, categorytitle: "vegetable"}
 Data[8] = categoryentity{grouponid: 8, merchandiseid: 1118166, categoryid: 1005, categorytitle: "home department store"}
 Data[9] = categoryentity{grouponid: 9, merchandiseid: 1117377, categoryid: 746, categorytitle: "fruit"}
 
 fmt. Println ("before random:", data)
 //If rand Seed (seed Int64), the random number will be the same every time
 rand.Seed(time.Now().Unix())
 //Rand Shuffle, randomize slices and return
 rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] })
 fmt. Println ("after random:", data)
}

3. assemble data and sort (scheme II)

The codes are as follows:

?
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
type CategoryEntity struct {
 Grouponid Int64 // group ID
 Merchandiseid Int64 // product ID
 Categoryid Int64 // category ID
 Categorytitle string // category name
}
 
func main() {
 data := make([]CategoryEntity, 10)
 Data[0] = categoryentity{grouponid: 0, merchandiseid: 1117891, categoryid: 726, categorytitle: "vegetable"}
 Data[1] = categoryentity{grouponid: 1, merchandiseid: 1110162, categoryid: 1505, categorytitle: "seasoning"}
 Data[2] = categoryentity{grouponid: 2, merchandiseid: 1117822, categoryid: 746, categorytitle: "fruit"}
 Data[3] = categoryentity{grouponid: 3, merchandiseid: 1115770, categoryid: 1408, categorytitle: "personal care"}
 Data[4] = categoryentity{grouponid: 4, merchandiseid: 1116528, categoryid: 732, categorytitle: "meat"}
 Data[5] = categoryentity{grouponid: 5, merchandiseid: 1116526, categoryid: 727, categorytitle: "snack food"}
 Data[6] = categoryentity{grouponid: 6, merchandiseid: 1117188, categoryid: 728, categorytitle: "grain and oil seasoning"}
 Data[7] = categoryentity{grouponid: 7, merchandiseid: 1117379, categoryid: 726, categorytitle: "vegetable"}
 Data[8] = categoryentity{grouponid: 8, merchandiseid: 1118166, categoryid: 1005, categorytitle: "home department store"}
 Data[9] = categoryentity{grouponid: 9, merchandiseid: 1117377, categoryid: 746, categorytitle: "fruit"}
 
 fmt. Println ("before random:", data)
 //If rand Seed (seed Int64), the random number will be the same every time
 rand.Seed(time.Now().Unix())
 length := len(data)
 for i := 0; i < length; i++ {
  exchange(data, rand.Intn(length), i)
 }
 
 fmt. Println ("after random:", data)
}
 
//Exchange data
func exchange(data []CategoryEntity, i, j int) {
 data[i], data[j] = data[j], data[i]
}

summary

The whole is simple, but there are two points to note:

1: How to use Rand in golang. If Rand is not used Seed (seed Int64), the random number will be the same every time

2: The first scheme adopts rand Shuffle+ an anonymous function that randomizes slices and returns them.

3: Scheme 2 uses the unique array exchange method of golang:

?
1
2
3
func exchange(data []CategoryEntity, i, j int) {
 data[i], data[j] = data[j], data[i]
}

This is the end of this article about the implementation of random sorting of golang arrays. For more information about random sorting of golang arrays, please search previous articles on developeppaper or continue to browse the following articles. I hope you will support developeppaper in the future!

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]