From 8925ae84c48de5b5ad5b5dbaf0a79990cacdc2ea Mon Sep 17 00:00:00 2001 From: Neo Morina Date: Wed, 22 Aug 2018 16:32:49 +0200 Subject: [PATCH] Update Taglist.php This sorting algorithm helps on showing the most similar labels at the first row. --- api/src/Etemplate/Widget/Taglist.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/src/Etemplate/Widget/Taglist.php b/api/src/Etemplate/Widget/Taglist.php index 78abb77bafd..9ac30c24163 100644 --- a/api/src/Etemplate/Widget/Taglist.php +++ b/api/src/Etemplate/Widget/Taglist.php @@ -79,6 +79,13 @@ public static function ajax_search() { $results[] = array('id' => $id, 'label' => $name); } + + usort($results, function ($a, $b) use ($query) { + similar_text($query, $a["label"], $percent_a); + similar_text($query, $b["label"], $percent_b); + return $percent_a === $percent_b ? 0 : ($percent_a > $percent_b ? -1 : 1); + }); + // switch regular JSON response handling off Api\Json\Request::isJSONRequest(false);