From 505a3b3b52794365cd5350c41a1d649a371b5210 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 17 Mar 2017 00:40:25 +0000
Subject: [PATCH] Improve autocomplete results sorting

---
 stops.php |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/stops.php b/stops.php
index b9b0132..86abb6b 100644
--- a/stops.php
+++ b/stops.php
@@ -46,18 +46,24 @@
 	
 	// Build structure for UI
 	$stop_list = [];
+	$query_lower = mb_strtolower($_GET['query'], 'UTF-8');
 	foreach($ids as $id) {
 		$stop_list[] = [
 			'id' => $id,
 			'name' => $stops[$id],
 			'type' => 'stop',
-			'relevance' => similar_text($_GET['query'], $stops[$id])
+			'relevance' => similar_text(
+				$query_lower,
+				mb_strtolower($stops[$id], 'UTF-8')
+			)
 		];
 	}
 	
 	// Sort stops by relevence
 	usort($stop_list, function($a, $b) {
-		return $b['relevance'] - $a['relevance'];
+		$rel = $b['relevance'] - $a['relevance'];
+		if($rel == 0) return strcasecmp($a['name'], $b['name']);
+		return $rel;
 	});
 	
 	// Return JSON

--
Gitblit v1.9.1