在排行榜中实现分数和时间排序 分数相同则按照时间排序
需求分析
- redis中zset的long为int64 转为十进制是19位
- 时间戳10位 毫秒时间戳13位 高位存分数7位 十进制 7位
- 8字节中拆分 最高位不可用 还剩43位 高22位存分数 低41位存时间戳
golang 代码
package main
import (
"fmt"
"sort"
"time"
)
var scores []int64
func main() {
sorts()
//bit()
}
func bit() {
fmt.Println(len(fmt.Sprintf("%d", 1<<9)))
fmt.Println(len(fmt.Sprintf("%d", 1<<41)))
fmt.Println(len(fmt.Sprintf("%d", 1<<23)))
fmt.Println(len("1000000"))
}
func sorts() {
Now := time.Now().UnixMilli()
for i := 1; i < 10; i++ {
scores = append(scores, toscore(int64(i+100000), Now))
}
sort.Slice(scores, func(i, j int) bool {
return scores[i] > scores[j]
})
for _, score := range scores {
fmt.Println(load(score))
//fmt.Println(score)
}
}
func toscore(point int64, periodEndTimestamp int64) (score int64) {
score = (score | point) << 41
score = score | periodEndTimestamp
return
}
func load(score int64) int64 {
return score >> 41
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。