slice_test.go 700 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package slice
  2. import (
  3. "fmt"
  4. cherryTime "github.com/cherry-game/cherry/extend/time"
  5. "math/rand"
  6. "sort"
  7. "testing"
  8. )
  9. func Test1211(t *testing.T) {
  10. type rank struct {
  11. Rank int32
  12. Score int32
  13. }
  14. var list []*rank
  15. rand.Seed(cherryTime.Now().Unix())
  16. for i := int32(1); i < 6; i++ {
  17. list = append(list, &rank{
  18. Rank: int32(len(list)),
  19. Score: rand.Int31n(100),
  20. })
  21. }
  22. sort.Slice(list, func(i, j int) bool {
  23. result := list[i].Score > list[j].Score
  24. if result {
  25. list[i].Rank = int32(j + 1)
  26. list[j].Rank = int32(i + 1)
  27. }
  28. return result
  29. })
  30. //for i, rankPlayer := range list {
  31. // rankPlayer.Rank = int32(i + 1)
  32. //}
  33. for i, v := range list {
  34. fmt.Println(i, v)
  35. }
  36. }