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] [package]
name = "xkcd-search" name = "xkcd-search"
version = "0.1.0" version = "0.1.1"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

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

Loading…
Cancel
Save