-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Sort by a new 'priority' parameter #26
Conversation
Bump minor, because there could be a minor change in behavior if some user already have a priority parameter on their posts
I've been looking forward to this feature for a long time. By the way, I found some theme such as hexo-theme-next, hexo-theme-melody etc. |
The parameter |
That's the intention.
|
But how about posts with same priority? It does not match the description. |
well, I just looked at the code again, and it is not what I intended... I believe the configuration config.index_generator.order_by can be used to order by priority, if a priority has been defined on all posts |
Yes, the issue is: When two or more posts have the same if(a.priority == b.priority) return b.date - a.date; |
@tomap I think you are talking about the stability of the sorting algorithm. Node.js 12+ uses stable sorting algorithms by default: https://v8.dev/features/stable-sort . Therefore, you can sort |
const posts = locals.posts; | ||
posts.data = posts.data.sort(function(a, b) { | ||
if(a.priority && b.priority) { | ||
if(a.priority == b.priority) return b.date - a.date; | ||
else return b.priority - a.priority; | ||
} | ||
else if(a.priority && !b.priority) { | ||
return -1; | ||
} | ||
else if(!a.priority && b.priority) { | ||
return 1; | ||
} | ||
else return b.date - a.date; | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const posts = locals.posts; | |
posts.data = posts.data.sort(function(a, b) { | |
if(a.priority && b.priority) { | |
if(a.priority == b.priority) return b.date - a.date; | |
else return b.priority - a.priority; | |
} | |
else if(a.priority && !b.priority) { | |
return -1; | |
} | |
else if(!a.priority && b.priority) { | |
return 1; | |
} | |
else return b.date - a.date; | |
}); | |
const posts = locals.posts.sort(config.index_generator.order_by); | |
posts.data.sort((a, b) => (b.sticky || 0) - (a.sticky || 0)); | |
Stable sorting requires Node.js 12+.
can you create another PR with your patch? This PR has been inactive for a while... |
Bump minor, because there could be a minor change in behavior
if some user already have a priority parameter
on their posts
Based upon the work of #6