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

Custom sorter function for search results #1106

Closed
TravisHardman opened this issue Feb 26, 2023 · 2 comments
Closed

Custom sorter function for search results #1106

TravisHardman opened this issue Feb 26, 2023 · 2 comments

Comments

@TravisHardman
Copy link

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

I've been trying to implement sorting for search results, which led me down a bit of a rabbit hole. I saw in your docs that "sorter" was not supported while searching, so I tried to add sortFn to my config.fuseOptions. That sort function ran, but the results in Choices.js weren't sorted as it dictated.

Finally, after a bit of debugging, I discovered this like in the Choices.js codebase, which hard-codes a sort-by-score function whenever the user is searching.

var filter = this._isSearching ? utils_1.sortByScore : this.config.sorter

This code overrides anything that happens in the Fuse sortFn, so sortByScore ends up being the only way to sort while searching.

Describe the solution you'd like

I would like a way to have a custom sort function while searching. I could see it working a few different ways:

  • A config option for searchSorter, which takes a function and defaults to sortByScore, just like the existing sorter option.
  • A config option for shouldSortWhileSearching, which uses the existing sorter config function when the user is searching.
  • Just falling back on the Fuse sortFn from the fuseOptions if it's provided.

Describe alternatives you've considered

I haven't found a workaround yet, but I figured I'd drop in this feature request, since any workaround does seem like it'll be quite hacky.

@TravisHardman
Copy link
Author

For what it's worth, the workaround I found was to handle the "search" event, then re-render the choices. This at least works for a single-select, which is all I needed.

choices = new Choices(select_el, {
  searchable: true,
  shouldSort: true,
  sorter: (a,b) => {
    // My sorting function here
  },
};
 
select_el.addEventListener('search', (e) => {
  choices._isSearching = false;
  choices._renderChoices(); // Uses the "sorter" function above because _isSearching is false
});

Hope that helps anyone who gets to this point, but it's pretty hacky. I stand by the feature request :)

@Xon
Copy link
Collaborator

Xon commented Aug 11, 2024

The call to sortByScore is required to use the fuse.js scores with the list of choices to render. I'll need to look into why a custom sortFn isn't applying as expected

Xon added a commit to Xon/Choices.js that referenced this issue Aug 11, 2024
@Xon Xon mentioned this issue Aug 22, 2024
9 tasks
@Xon Xon closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants