fmoo / react-typeahead

Pure react-based typeahead and typeahead-tokenizer
ISC License
677 stars 230 forks source link

Sort results by relevance, not alphabetical order #119

Open yantakus opened 9 years ago

yantakus commented 9 years ago

Is there a way to sort results by relevance.

For example, if or options list is ['Canada', 'Abacan', 'Bolcan']. I type "Can" and see:

Abacan
Bolcan
Canada

But "Canada" is the most relevant result, because it starts with our inputValue.

I have a sort function to handle it:

arr.sort((a, b) => {
 if (a.indexOf(inputValue) > b.indexOf(inputValue)) return 1;
 else if (a.indexOf(inputValue) < b.indexOf(inputValue)) return -1;
 else return 0;
})

How can I implement it?

kjs3 commented 8 years ago

:+1: