Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于article-rpc logic层的cmp包以及 slices包引用问题 #31

Open
diy0663 opened this issue Nov 18, 2023 · 2 comments
Open

关于article-rpc logic层的cmp包以及 slices包引用问题 #31

diy0663 opened this issue Nov 18, 2023 · 2 comments

Comments

@diy0663
Copy link

diy0663 commented Nov 18, 2023

beyond/application/article/rpc/internal/logic/articleslogic.go
里面的cmp包以及 slices 是哪来的?

@cpf2021-gif
Copy link
Contributor

后面加的,使用mapreduce获取文章列表是无序的,因为返回的文章是需要排序,所以使用了slices.SortFunc对获取的文章进行排序,排序的方法有很多种,你也可以使用其他的排序方法
关于这个部分的改动可以查看这个#23

@toweron2
Copy link

toweron2 commented Jul 1, 2024

slices.SortFunc 是 go 1.21版本增加的方法, 相关连接 Go 1.21 Release Notes
旧版本可使用以下实现切片排序

var cmpFunc func(i, j int) bool
if sortField == "like_num" {
cmpFunc = func(i, j int) bool {
		return articles[i].LikeNum > articles[j].LikeNum
	}
} else {
	cmpFunc = func(i, j int) bool {
		return articles[i].PublishTime.Unix() > articles[j].PublishTime.Unix()
	}
}
sort.Slice(articles, cmpFunc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants