0

The problem at hand is that the search suggestions are not ordered by same starting characters as can be seen in the picture:'ne' should be at the very top, yet it is at the very bottom

How can I fix this problem?

The following is my code`$(document).ready(function() { //var queries = bank;

var queries = ['there is no need', 'need', 'no need', 'ne'];

//Dataset defined in index.php
// *****
//(NOTE: Typehead works by the order of the elements in dataset, thus
//          they are ordered in the database first based on count)

//Constructing the suggestion engine
var queries = new Bloodhound(
    {
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: queries
    });

// Initializing the typeahead (.typehead is the selector having what's currently
//                                  being typed)
$('.typeahead').typeahead(
    {
        hint: true,
        highlight: true, /* Enable substring highlighting */
        minLength: 1 /* Specify minimum characters required for showing result */
    },
    {
        name: 'queries',
        source: queries
    });

}); `

M. Shoaib
  • 1
  • 1

1 Answers1

0

If the sorter property is initialized with a function, Bloodhound will use it to order the matched entries, like below:

var queries = new Bloodhound(
{
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: queries,
    sorter: function(a, b) {
        if (a < b) {
            return -1;
        }
        else if (a > b) {
            return 1;
        }
        else return 0;
    }
});

So, just extend the initialization of Bloodhound, and you are good to go.

Vitali Climenco
  • 1,294
  • 1
  • 12
  • 18