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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use core::cmp::Ordering;

use alloc::vec::IntoIter;

/// An extension trait that provides the [`sorted`] method and friends for
/// iterators.
///
/// [`sorted`]: IterSorted::sorted
#[cfg_attr(docsrs, doc(cfg(feature = "sorted")))]
pub trait IterSorted: Iterator {
    /// Sorts the iterator.
    ///
    /// Simply collects into a [`Vec`] and sorts it using [`slice::sort`].
    fn sorted(self) -> IntoIter<Self::Item>
    where
        Self: Sized,
        Self::Item: Ord,
    {
        let mut v = Vec::from_iter(self);
        v.sort();
        v.into_iter()
    }

    /// Sorts the iterator with a comparator function.
    ///
    /// Simply collects into a [`Vec`] and sorts it using [`slice::sort_by`].
    fn sorted_by<F>(self, cmp: F) -> IntoIter<Self::Item>
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> Ordering,
    {
        let mut v = Vec::from_iter(self);
        v.sort_by(cmp);
        v.into_iter()
    }

    /// Sorts the iterator with a key extraction function.
    ///
    /// Simply collects into a [`Vec`] and sorts it using
    /// [`slice::sort_by_key`].
    fn sorted_by_key<K, F>(self, f: F) -> IntoIter<Self::Item>
    where
        Self: Sized,
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        let mut v = Vec::from_iter(self);
        v.sort_by_key(f);
        v.into_iter()
    }

    /// Sorts the iterator with a key extraction function.
    ///
    /// Simply collects into a [`Vec`] and sorts it using
    /// [`slice::sort_by_cached_key`].
    fn sorted_by_cached_key<K, F>(self, f: F) -> IntoIter<Self::Item>
    where
        Self: Sized,
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        let mut v = Vec::from_iter(self);
        v.sort_by_cached_key(f);
        v.into_iter()
    }

    /// Sorts the iterator, but might not preserve the order of equal elements.
    ///
    /// Simply collects into a [`Vec`] and sorts it using
    /// [`slice::sort_unstable`].
    fn sorted_unstable(self) -> IntoIter<Self::Item>
    where
        Self: Sized,
        Self::Item: Ord,
    {
        let mut v = Vec::from_iter(self);
        v.sort_unstable();
        v.into_iter()
    }

    /// Sorts the iterator with a comparator function, but might not preserve
    /// the order of equal elements.
    ///
    /// Simply collects into a [`Vec`] and sorts it using
    /// [`slice::sort_unstable_by`].
    fn sorted_unstabled_by<F>(self, cmp: F) -> IntoIter<Self::Item>
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> Ordering,
    {
        let mut v = Vec::from_iter(self);
        v.sort_unstable_by(cmp);
        v.into_iter()
    }

    /// Sorts the iterator with a key extraction function, but might not
    /// preserve the order of equal elements.
    ///
    /// Simply collects into a [`Vec`] and sorts it using
    /// [`slice::sort_unstable_by_key`].
    fn sorted_unstable_by_key<K, F>(self, f: F) -> IntoIter<Self::Item>
    where
        Self: Sized,
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        let mut v = Vec::from_iter(self);
        v.sort_unstable_by_key(f);
        v.into_iter()
    }
}

impl<I: ?Sized> IterSorted for I where I: Iterator {}