Created
January 27, 2015 22:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/hjr3/eaa3c53b29f6191ba265 for a better way