golang 实现redis 排行榜同分值情况下按时间升序排序
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
}