From 89eaae55c347a4ee5c82c58665396bc87bb018e7 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 29 Mar 2018 00:57:56 +0200 Subject: [PATCH] Introduce sorting for articles --- .../wikivoyage/data/WikivoyageDbHelper.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java index 810191ebf5..22422ceb11 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java @@ -2,7 +2,12 @@ package net.osmand.plus.wikivoyage.data; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import net.osmand.Collator; +import net.osmand.CollatorStringMatcher; import net.osmand.IndexConstants; +import net.osmand.OsmAndCollator; +import net.osmand.PlatformUtil; +import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; @@ -13,6 +18,8 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -62,12 +69,15 @@ public class WikivoyageDbHelper { private final OsmandApplication application; + private Collator collator; + public WikivoyageDbHelper(OsmandApplication application) { this.application = application; + collator = OsmAndCollator.primaryCollator(); } @NonNull - public Collection search(String searchQuery) { + public Collection search(final String searchQuery) { List res = new ArrayList<>(); SQLiteConnection conn = openConnection(); if (conn != null) { @@ -84,7 +94,29 @@ public class WikivoyageDbHelper { conn.close(); } } - return groupSearchResultsByCityId(res); + List list = new ArrayList(groupSearchResultsByCityId(res)); + + Collections.sort(list, new Comparator() { + + @Override + public int compare(WikivoyageSearchResult o1, WikivoyageSearchResult o2) { + boolean c1 = CollatorStringMatcher.cmatches(collator, searchQuery, o1.articleTitle.get(0), + StringMatcherMode.CHECK_ONLY_STARTS_WITH); + boolean c2 = CollatorStringMatcher.cmatches(collator, searchQuery, o2.articleTitle.get(0), + StringMatcherMode.CHECK_ONLY_STARTS_WITH); + if(c1 == c2) { + return collator.compare(o1.articleTitle.get(0), o2.articleTitle.get(0)); + } + if(c1) { + return -1; + } else if(c2) { + return 1; + } + return 0; + } + }); + + return list; } private Collection groupSearchResultsByCityId(List res) {