Sort typeahead suggestions with the exact input at top

前端 未结 2 466
独厮守ぢ
独厮守ぢ 2021-02-05 23:10

I am using Twitter typeahead.js 0.10.5 as a suggestion engine. It works great, with one exception, I can\'t sort the list of suggestions the way I want.

As an example:

2条回答
  •  一生所求
    2021-02-05 23:58

    To sort all matches by closeness to the input, you can take the Levenshtein distance of a and b. I just implemented this using fast-levenshtein and it works and performs great.

            sorter: function(a, b) {
                var input_string = $(selector).val();
                return levenshtein.get(a.key, input_string) - levenshtein.get(b.key, input_string);
            }
    

提交回复
热议问题