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

Stable sort for left nav #374

Merged
merged 1 commit into from
Oct 23, 2024
Merged

Stable sort for left nav #374

merged 1 commit into from
Oct 23, 2024

Conversation

heathd
Copy link
Contributor

@heathd heathd commented Oct 23, 2024

The existing sort algorithm for the left menu allows the sort order to be controlled by specifying weights for items however it does not preserve the sort-order of items:

  • which have the same weight as one another
  • which have no weight assigned

The ruby builtin function sort_by is not guaranteed to be stable. What this means is that if items do not have an explicit weight specified they can randomly shuffle around between each execution of the rendering process which makes it harder than necessary to manage/curate the order of the left menu.

What’s changed

This PR changes the algorithm to respect the ordering of items as they appear in the middleman resources tree. The algorithm is:

  1. items are sorted by weight first
  2. items with equal weight are sorted respecting the natural ordering in the resources tree
  3. items without a weight specified appear last and are sorted respecting the natural ordering in the resources tree

The overall outcome is that the left nav ordering does not need to be completely controlled by weights.

This change does not break any existing sites because, if they did not control the ordering entirely by specifying weights, then the ordering would have been non-deterministic.

User need

This makes it easier to manage the ordering of items in the left nav

try to avoid left menu jumping around
Copy link
Contributor

@marcotranchino marcotranchino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@heathd heathd merged commit d53ea38 into main Oct 23, 2024
3 checks passed
@heathd heathd deleted the stable-sort-left-nav branch October 23, 2024 11:19
@kr8n3r
Copy link
Contributor

kr8n3r commented Oct 23, 2024

no changelog?

kr8n3r added a commit that referenced this pull request Oct 23, 2024
- [Fix missing manifest.json from govuk-frontend](#376)
- [Stable soft for left navigation](#374)
@kr8n3r kr8n3r mentioned this pull request Oct 23, 2024
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.

3 participants