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 best match first #446

Open
falch0n opened this issue Feb 11, 2021 · 3 comments
Open

Sort best match first #446

falch0n opened this issue Feb 11, 2021 · 3 comments

Comments

@falch0n
Copy link

falch0n commented Feb 11, 2021

As far as I know the sorting is done by the first letter of the result. How do I sort on matching the "best" result, meaning the most exact match starting from the first letter.

i.e. Now when I search for 'shoe' (with sort enabled), I get

  • Another shoe
  • Outlet shoe
  • Shoe
  • Sport shoe
  • Reshoev8er

Since 'Shoe' is the "best" match in my opinion, I'd like to place it first in order. What's the best way to accomplish this?

@falch0n
Copy link
Author

falch0n commented Mar 2, 2021

Is there anyone who could help me out? 🙂

@sumitzarkar
Copy link

sumitzarkar commented Apr 2, 2021

@falch0n

Here is the solution for this.

  • Override sort method
  • Sortting should be done on the index of the inputPhrase in the element

Here is the working sample for your data

		var options = {
			data: ['Another shoe', 'Outlet shoe', 'Shoe', 'Sport shoe', 'Reshoev8er', 'Another1 shoe'],
			list: {
				match: { enabled: true },
				sort: {
					enabled: true,
					method: function (aValue, bValue) {
						var inputPhrase = $('#simple').val();
						var a = aValue.indexOf(inputPhrase);
						var b = bValue.indexOf(inputPhrase);
						if (a < b) {
							return -1;
						}
						if (a > b) {
							return 1;
						}
						return 0;
					}
				}
			}
		};
		$('#simple').easyAutocomplete(options);

autocompleteIssue

@pawelczak is this the correct way to tackle this or do you see any other solution.

@ecc521
Copy link

ecc521 commented Mar 4, 2022

This seems to be like an incredibly obvious feature to implement - it is quite easy to implement, should not significantly impact performance, and would result in dramatically better autocompletes.

The SurveyJS example demonstrates this issue perfectly:

Screen Shot 2022-03-04 at 5 12 08 PM

No user starts typing in "Peugeot" with a "t" - they would start typing Toyota with a "t". And if all t's are assumed equal, even when they are clearly not, then Toyota has two and should therefore be put first.

Ultimately, this issue makes this library unusable with large autocomplete sets, as users need to type in a significant portion of the entire item before autocorrect even displays the item they want in the list.

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

No branches or pull requests

3 participants