| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package slice
- import (
- "fmt"
- cherryTime "github.com/cherry-game/cherry/extend/time"
- "math/rand"
- "sort"
- "testing"
- )
- func Test1211(t *testing.T) {
- type rank struct {
- Rank int32
- Score int32
- }
- var list []*rank
- rand.Seed(cherryTime.Now().Unix())
- for i := int32(1); i < 6; i++ {
- list = append(list, &rank{
- Rank: int32(len(list)),
- Score: rand.Int31n(100),
- })
- }
- sort.Slice(list, func(i, j int) bool {
- result := list[i].Score > list[j].Score
- if result {
- list[i].Rank = int32(j + 1)
- list[j].Rank = int32(i + 1)
- }
- return result
- })
- //for i, rankPlayer := range list {
- // rankPlayer.Rank = int32(i + 1)
- //}
- for i, v := range list {
- fmt.Println(i, v)
- }
- }
|