Fix regular search not being sorted

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent 2ba3cf4cd1
commit fbecddcfdc
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package]
name = "xkcd-search"
version = "0.1.0"
version = "0.1.1"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"

@ -10,17 +10,27 @@ pub async fn search(query: &str) -> XKCDResult<Vec<(String, u32)>> {
.into_iter()
.map(|s| s.to_lowercase())
.collect();
let entries = archive
let mut entries: Vec<(usize, String, u32)> = archive
.into_iter()
.filter(|(key, _)| {
words
.filter_map(|(key, id)| {
let score = words
.iter()
.find(|w| key.to_lowercase().contains(*w))
.is_some()
.filter(|w| key.to_lowercase().contains(*w))
.count();
if score <= 0 {
None
} else {
Some((score, key, id))
}
})
.collect();
entries.sort_by_key(|(s, _, _)| *s);
entries.reverse();
Ok(entries)
Ok(entries
.into_iter()
.map(|(_, title, id)| (title, id))
.collect())
}
/// Searches for a comic with a fuzzy compare function that assigns a score

Loading…
Cancel
Save