Skip to content

Instantly share code, notes, and snippets.

@hjr3
Created January 27, 2015 22:55
Show Gist options
  • Save hjr3/b535700b92fed0325a21 to your computer and use it in GitHub Desktop.
Save hjr3/b535700b92fed0325a21 to your computer and use it in GitHub Desktop.
How to work around the fact that there is no Iterator::sort_by() method.
fn compute_matches(choices: &Vec<String>, query: &str) -> Vec<String> {
let unsorted_matches: Vec<(&String, f64)> = choices.iter().map(|choice| {
(choice, score(choice.as_slice(), query))
}).filter(|&(_choice, score)| {
score > 0.0
}).collect();
let mut matches = unsorted_matches.clone();
matches.sort_by(|&(_choice_a, score_a), &(_choice_b, score_b)| {
if score_a > score_b {
Ordering::Less
} else if score_a < score_b {
Ordering::Greater
} else {
Ordering::Equal
}
});
matches.iter().map(|&(choice, _score)| {
// TODO clone here is not ideal, but i cannot
// yet figure out how to borrow through all of this
// correctly
choice.clone()
}).collect()
}
@hjr3
Copy link
Author

hjr3 commented Jan 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment