question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add alphabetical and easy-to-use custom sorting options to storySort parameter

See original GitHub issue

Is your feature request related to a problem? Please describe.

The current implementation of storySort only takes a function that can be used to sort stories. There’s literally nothing preventing you from doing any sorting EXCEPT that’s it’s really hard to do. There is a readme that explains how to do alphabetical sorting:

addParameters({
  options: {
    storySort: (a, b) =>
      a[1].kind === b[1].kind ? 0 : a[1].id.localeCompare(b[1].id, { numeric: true }),
  },
});

(BTW, there’s a bug in that example, which just shows how hard it is to implement correctly.)

Describe the solution you’d like

In addition to the current API of:

storySort: (a, b) => { /* return -1, 0, or 1 */ },

Alternatively, a user could provide an object to storySort that looks like this:

storySort: {
  method: 'alphabetical', // Defaults to 'configure', i.e. just use Storybook's configure() ordering.
  order: []; // An array of story kinds to order by.
  locales: 'en-US', // A locales string to use when sorting alphabetically.
},

The order array could look like this: ['Intro', 'Pages', 'Components'] and that would sort story kinds that start with Intro, Pages, or Components (in that order) before any story kinds that don’t start with one of those 3 items.

You could also do a nested array so that you could order 2nd-level story kinds. e.g.

[
  'Intro',
  'Pages', 
  [
    'Home',
    'Login',
    'Admin',
  ],
  'Components',
]

Which would result in this ordering:

  1. Intro and Intro/* kinds.
  2. Pages
  3. Pages/Home and Pages/Home/*
  4. Pages/Login and Pages/Login/*
  5. Pages/Admin and Pages/Admin/*
  6. Pages/*
  7. Components and Components/*
  8. Lastly, everything else.

Note that the order option is independent of the method option; you can sort the * stories by configure() or by method: 'alphabetical'.

Describe alternatives you’ve considered

The current method is flexible, but difficult to use. This proposal is about the 4th iteration of code I’ve used on a Storybook project.

Are you able to assist bring the feature to reality? Yep. I’ve written a feature branch for it. With tests. And docs.

Additional context Add any other context or screenshots about the feature request here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JohnAlbincommented, Dec 18, 2019

The custom hierarchy separator is deprecated in 5.3 in favor of just using /.

1reaction
helloalexdoddcommented, Feb 4, 2020

Hey I’m not able to contribute to this at this time, but I would love to see something like this added in. Sorting stories right now is unnecessarily #$@&ing hard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Naming components and hierarchy - Storybook - JS.ORG
To sort your stories alphabetically, set method to 'alphabetical' and optionally set the locales string. To sort your stories using a custom list,...
Read more >
Sorting in Storybook: Possible to force an order for nested ...
I'm currently sorting stories like this: options: { storySort: { method: 'alphabetical', order: ['Introduction ...
Read more >
Extra Product Sorting Options for WooCommerce
When you create a customized sorting order, you lose the ability to sort products alphabetically. This plugin gives you the ability to add...
Read more >
easy admin collectionfield array (1 items) - You.com - You.com
EasyAdmin Array Field Edit this page Basic Information Options This field displays ... and easy-to-use custom sorting options to storySort parameter#9185.
Read more >
How to sort stories alphabetically in Storybook (6.2)
file:.storybook/preview.js export const parameters = { options: { storySort: (a, b) => { ... Tagged with storybook, react, javascript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found