Last active
August 29, 2015 14:14
-
-
Save hjr3/eaa3c53b29f6191ba265 to your computer and use it in GitHub Desktop.
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
let new_col: Vec<&str> = col.iter().map(|&a| { | |
(a, 1) | |
}).sort_by(|&a, &b| { | |
a.1.cmp(&b.1) | |
}).map(|(col, _score)| col ).collect(); |
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
extern crate core; | |
extern crate collections; | |
use core::cmp::Ordering; | |
use collections::vec::IntoIter; | |
pub trait SortIterator<A, I>: Iterator { | |
fn sort_by<F>(self, compare: F) -> IntoIter<A> where F: FnMut(&A, &A) -> Ordering; | |
} | |
impl<A, I: Iterator<Item=A>> SortIterator<A, I> for I { | |
fn sort_by<F>(self, compare: F) -> IntoIter<A> | |
where F: FnMut(&A, &A) -> Ordering, | |
{ | |
let mut v: Vec<A> = self.collect(); | |
v.sort_by(compare); | |
v.into_iter() | |
} | |
} | |
fn main() { | |
let col = ["b", "a"]; | |
let new_col: Vec<&str> = col.iter().sort_by(|&a, &b| { | |
a.cmp(b) | |
}).map(|&a| a ).collect(); | |
for i in new_col.iter() { | |
println!("i = {}", i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment