Skip to content

Conversation

@neomorina
Copy link
Contributor

This sorting algorithm helps on showing the most similar labels at the first row.

This sorting algorithm helps on showing the most similar labels at the first row.
@neomorina
Copy link
Contributor Author

neomorina commented Sep 4, 2018

For much better results i actually recommend implementing this third party library through Composer https://github.com/wyndow/fuzzywuzzy
The code would look like this.

use FuzzyWuzzy\Fuzz;      
$fuzz = new Fuzz();

usort($results, function ($a, $b) use ($query, $fuzz) {
   $a_label = is_array($a['label']) ? $a['label']['label'] : $a['label'];
   $b_label = is_array($b['label']) ? $b['label']['label'] : $b['label'];

   $percent_a = $fuzz->tokenSetRatio($query, $a_label);
   $percent_b = $fuzz->tokenSetRatio($query, $b_label);

   return $percent_a === $percent_b ? 0 : ($percent_a > $percent_b ? -1 : 1);
});

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

Successfully merging this pull request may close these issues.

1 participant