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

Sort by a new sticky parameter #51

Merged
merged 3 commits into from
Jun 12, 2020
Merged

Sort by a new sticky parameter #51

merged 3 commits into from
Jun 12, 2020

Conversation

stevenjoezhang
Copy link
Member

@stevenjoezhang stevenjoezhang commented Apr 26, 2020

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
Copy link
Member

SukkaW commented Apr 27, 2020

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

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

@coveralls
Copy link

coveralls commented Apr 27, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 4203610 on sticky into 88843c2 on master.

@stevenjoezhang
Copy link
Member Author

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.

{source: 'foo', slug: 'foo', date: 1e8, order: 0},
{source: 'bar', slug: 'bar', date: 1e8 + 1, order: 10},
{source: 'baz', slug: 'baz', date: 1e8 - 1, order: 1}

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

@stevenjoezhang stevenjoezhang requested a review from a team June 12, 2020 14:59
README.md Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

None yet

4 participants