Skip to content

Commit

Permalink
Sort app results first if they start with the query (#4044)
Browse files Browse the repository at this point in the history
  • Loading branch information
neuos authored Feb 3, 2024
1 parent 24f4703 commit 1c69639
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lawnchair/src/app/lawnchair/search/LawnchairAppSearchAlgorithm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class LawnchairAppSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm(c
return apps.asSequence()
.filter { StringMatcherUtility.matches(queryTextLower, it.title.toString(), matcher) }
.filterHiddenApps(queryTextLower)
.sortedWith(queryComparator(queryTextLower))
.take(maxResultsCount)
.toList()
}
Expand All @@ -182,16 +183,18 @@ class LawnchairAppSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm(c
val filteredApps = apps.asSequence()
.filterHiddenApps(queryTextLower)
.toList()
val matches = FuzzySearch.extractSorted(
val matches = FuzzySearch.extractTop(
queryTextLower,
filteredApps,
{ it.sectionName + it.title },
WeightedRatio(),
65,
)

return matches.take(maxResultsCount)
return matches
.map { it.referent }
.sortedWith(queryComparator(queryTextLower))
.take(maxResultsCount)
}

private fun Sequence<AppInfo>.filterHiddenApps(query: String): Sequence<AppInfo> {
Expand All @@ -206,4 +209,11 @@ class LawnchairAppSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm(c
filter { it.toComponentKey().toString() !in hiddenApps }
}
}

private fun queryComparator(query: String): Comparator<AppInfo> {
return compareBy(
{ it.title.toString().startsWith(query, true).not() },
{ it.title.toString() },
)
}
}

0 comments on commit 1c69639

Please sign in to comment.