hexojs / hexo-generator-index

Index generator plugin for Hexo.
MIT License
51 stars 44 forks source link

Sort by a new `sticky` parameter #51

Closed stevenjoezhang closed 4 years ago

stevenjoezhang commented 4 years ago

Test cases / docs will be updated later.

Node.js 12+ uses stable sorting algorithms by default, thus timsort is only used for Node.js 10 compatibility. It should be replaced after Node.js 10 EOL. https://v8.dev/features/stable-sort

// Node.js 10
sort(posts.data, (a, b) => (b.sticky || 0) - (a.sticky || 0));

// Node.js 12+
posts.data.sort((a, b) => (b.sticky || 0) - (a.sticky || 0));
SukkaW commented 4 years ago

https://github.com/mziccard/node-timsort

There is a high performance tim-sort implementation that could be used as an alternative.

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 420361065ad5df94b06037aa134e7df0b5f421ba on sticky into 88843c2ae12d7ca96865de720e1dffbe96bb5252 on master.

stevenjoezhang commented 4 years ago

Another approach is

const posts = locals.posts.sort(`${config.index_generator.order_by} -sticky`);

However, the current implementation of warehouse does not support setting default values for sort (e.g. sticky = 0), thus the sorting will not take effect unless every post has a sticky or order attribute.

https://github.com/hexojs/hexo-generator-index/blob/88843c2ae12d7ca96865de720e1dffbe96bb5252/test/index.js#L20-L22

https://github.com/hexojs/warehouse/blob/c24cf938152ea0c3fd04a9bac0ca1db8e661bdb2/lib/schema.js#L60