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.

Search without sorting

See original GitHub issue

Hi there,

I have a large select with years. Let’s say 1900 until 2018. And add them with setChoices(). The items are already sorted so they do not need to be sorted again.

Two things:

  1. If I add the years as integers the search is not giving any results. If I add them as strings it is working. Not a big deal and that is not what this issue is about.
  2. If I search with string 20 the first result is 2009 and after that is is nicely sorted as expected: 2018, 2017, 2016, 2015, etc. The same thing happens when I search on 19: the first record is 1949 and after that the results are 1999, 1998 is missing (is somewhere below) 1997, 1996, 1995, 1994, etc.

I can’t see why this is happening. As far as I can see the results from fuse are correctly sorted (read: unchanged).

new Choices(document.getElementById('select'), {
          shouldSort: false,
          silent: true,
          searchResultLimit: 100,
          itemSelectText: '',
          fuseOptions: {
              shouldSort: false,
              includeScore: false,
              includeMatches: false,
              threshold: 0,
              location: 0,
              distance: 100,
              maxPatternLength: 32,
              minMatchCharLength: 1,
              keys: ["value"]
          }
});

See also this example https://plnkr.co/edit/d1hsGPour408YC5OqVSf?p=preview v3.0.3

Is there any way to just keep the sorting as it is?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
khawkins98commented, Apr 30, 2018

We’re still on v2.7.8, but here’s an edit we’re thinking about making to our environment (eventually doing a PR):

  1. Add a new option secondarySortByAlpha
  2. Modifying sortByScore to be:
  var sortByScore = exports.sortByScore = function sortByScore(a, b) {
    if (this.config.secondarySortByAlpha) {
      if (Math.round(a.score * 100) / 100 == Math.round(b.score * 100) / 100) {
        return sortByAlpha(a,b);
      }
    }
    return a.score - b.score;
  };

So, here, in cases of a close match (two decimal points) they would be sorted alphabetically – which I imagine would do similarly for number labels.

Before: image

After: image

0reactions
stale[bot]commented, Oct 27, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fastest way to search for an element in unsorted array
You start off by sorting the array using the merge sort algorithm, then you use binary search to find the element. Both algoro...
Read more >
How do I search for an element in an array without sorting?
The most straightforward way is to do exactly that: take one pass to identify the largest element, and a second pass to identify...
Read more >
Rearrange Array to find K using Binary Search algorithm ...
Rearrange Array to find K using Binary Search algorithm without sorting ; Find the mid point of the current range [L, R] as...
Read more >
Excel Lookup without sorting Table - Possible?
Lookup requires that the table (lookup vector) be sorted by the lookup field (value). Is there a method or other formula that does...
Read more >
Algorithms: Searching and Sorting Cheatsheet
Binary search performs the search for the target within a sorted array. Hence, to run a binary search on a dataset, it must...
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