1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::cmp::Ordering;

/**
Shorthand for collecting and sorting an iterator.
*/
pub trait SortedIterator<E>: Iterator<Item=E> + Sized where E: Ord {
    /**
Returns a `Vec` with the elements of the input iterator in sorted order.
    */
    fn sorted(self) -> Vec<E>;

    /**
Returns a `Vec` with the elements of the input iterator in sorted order, as specified by a comparison function.
    */
    fn sorted_by<F: FnMut(&E, &E) -> Ordering>(self, compare: F) -> Vec<E>;
}

impl<It, E> SortedIterator<E> for It where It: Iterator<Item=E>, E: Ord {
    fn sorted(self) -> Vec<E> {
        let mut v = self.collect::<Vec<_>>();
        v.sort();
        v
    }

    fn sorted_by<F: FnMut(&E, &E) -> Ordering>(self, compare: F) -> Vec<E> {
        let mut v = self.collect::<Vec<_>>();
        v.sort_by(compare);
        v
    }
}

#[test]
fn test_sorted() {
    let v = vec![1usize, 3, 2, 0, 4];
    let s = v.into_iter().sorted();
    assert_eq!(s, vec![0usize, 1, 2, 3, 4]);
}

#[test]
fn test_sorted_by() {
    let v = vec![1usize, 3, 2, 0, 4];
    let s = v.into_iter().sorted_by(|a,b| (!*a).cmp(&!*b));
    assert_eq!(s, vec![4, 3, 2, 1, 0usize]);
}