golang 根据分数计算排名,同分数排名相同
package main
import (
"fmt"
"sort"
)
type userInfo struct {
Uid int64 \/\/uid
Score int64 \/\/分数
Rank int \/\/排名
}
type Score []userInfo
func (s Score) Len() int { return len(s) }
func (s Score) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Score) Less(i, j int) bool { return s[i].Score > s[j].Score } \/\/ 从大到小排序
func main() {
users := []userInfo{}
scores := []int64{0, 2, 2, 3, 0, 0, 0, 0, 0, 0,0}
fmt.Println(scores)
for i := 1; i <= 10; i++ {
users = append(users, userInfo{
Uid: int64(i),
Score: scores[i],
})
}
sort.Sort(Score(users))
fmt.Printf("%+v \n