Skip to content

Instantly share code, notes, and snippets.

@hjr3
Last active August 29, 2015 14:14
Show Gist options
  • Save hjr3/eaa3c53b29f6191ba265 to your computer and use it in GitHub Desktop.
Save hjr3/eaa3c53b29f6191ba265 to your computer and use it in GitHub Desktop.
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();
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