【本文正在参加金石计划附加挑战赛——第一(或二、三、四)期命题】
好久没来 掘金写做了,还记得之前写做,掘金奖励了一个马克杯,但是最近有一些杂七杂八的事情,也没时间来分享,今天刚好收到掘金的消息,也是有时间吧,来分享一个实用的小功能吧
没有没有长篇大论,直接上 干货
import (
"fmt"
"sort"
)
func main() {
s := []int64{1, 23, 4, 5, 67, 8}
q := struct {
S []int64
N string
}{}
q.S = s
q.N = "123123123"
sort.Sort(IntSlice(q.S))
fmt.Println(q)
}
type IntSlice []int64
func (s IntSlice) Len() int {
return len(s)
}
func (s IntSlice) Less(i, j int) bool {
return s[i] > s[j]
}
func (s IntSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
这个是 基础版本的,你需要在使用之前 提前设置
我们理解好这个基础版本就可以去使用进阶版本了
func main() {
titleList := []int{1, 4, 6, 7, 8, 43, 8}
sort.Slice(titleList, func(i, j int) bool {
return titleList[i] < titleList[j]
})
fmt.Println(titleList)
}
这个就是 进阶版本的,我们可以在项目中使用这个小功能了