blog-hexo/themes/hexo-theme-cosy/scripts/sort_posts.js

17 lines
552 B
JavaScript
Raw Normal View History

2023-10-19 21:55:22 +08:00
hexo.extend.helper.register('sort_posts_by_top', function (posts) {
2023-11-11 19:22:04 +08:00
2023-10-19 21:55:22 +08:00
return posts.slice().sort(function (a, b) {
2023-11-11 19:22:04 +08:00
var topA = a.top !== undefined ? a.top : Number.MAX_SAFE_INTEGER;
var topB = b.top !== undefined ? b.top : Number.MAX_SAFE_INTEGER;
2023-10-19 21:55:22 +08:00
2023-11-11 19:22:04 +08:00
// 根据 top 字段从小到大降序排列
2023-10-19 21:55:22 +08:00
if (topA < topB) return -1;
if (topA > topB) return 1;
2023-11-11 19:22:04 +08:00
// 如果 top 字段相同或不存在,则根据日期从新到旧排序
var dateA = new Date(a.date);
var dateB = new Date(b.date);
return dateB - dateA;
2023-10-19 21:55:22 +08:00
});
});