当前位置:主页 > 脚本语言 > Golang >

golang 数组随机排序的实现

时间:2022-09-14 09:16:10 | 栏目:Golang | 点击:

前言

目前接到一个推荐数据的需求,需要将数据库中获取到的数据进行随机排序后返回给用户。考虑了一下,有两种使用方式,一种是通过数据库 order by rand() ,还有一种就是本文需要使用到的代码处理

具体实现步骤如下

1.引入库

代码如下:

import (
 "fmt"
 "math/rand"
 "time"
)

2.组装数据并排序(方案一)

代码如下:

type CategoryEntity struct {
 GrouponId     int64  //团ID
 MerchandiseId int64  //商品ID
 CategoryId    int64  //分类ID
 CategoryTitle string //分类名称
}

func main() {
 data := make([]CategoryEntity, 10)
 data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "蔬菜"}
 data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "调料调味"}
 data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "水果"}
 data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "个人护理"}
 data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "肉"}
 data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "休闲食品"}
 data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "粮油调味"}
 data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "蔬菜"}
 data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "居家百货"}
 data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "水果"}

 fmt.Println("随机前:", data)
 //如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样
 rand.Seed(time.Now().Unix())
 //采用rand.Shuffle,将切片随机化处理后返回
 rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] })
 fmt.Println("随机后:", data)
}

3.组装数据并排序(方案二)

代码如下:

type CategoryEntity struct {
 GrouponId     int64  //团ID
 MerchandiseId int64  //商品ID
 CategoryId    int64  //分类ID
 CategoryTitle string //分类名称
}

func main() {
 data := make([]CategoryEntity, 10)
 data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "蔬菜"}
 data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "调料调味"}
 data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "水果"}
 data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "个人护理"}
 data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "肉"}
 data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "休闲食品"}
 data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "粮油调味"}
 data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "蔬菜"}
 data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "居家百货"}
 data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "水果"}

 fmt.Println("随机前:", data)
 //如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样
 rand.Seed(time.Now().Unix())
 length := len(data)
 for i := 0; i < length; i++ {
  exchange(data, rand.Intn(length), i)
 }

 fmt.Println("随机后:", data)
}

// 交换数据
func exchange(data []CategoryEntity, i, j int) {
 data[i], data[j] = data[j], data[i]
}

总结

整体比较简单,但是需要注意的有两点:

1:golang中 rand的使用方式,如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样

2:方案一种采用rand.Shuffle+匿名函数,将切片随机化处理后返回。

3:方案二使用golang特有的数组交换方式:

func exchange(data []CategoryEntity, i, j int) {
 data[i], data[j] = data[j], data[i]
}

您可能感兴趣的文章:

相关文章