@@ -2187,6 +2187,101 @@ pub trait Itertools : Iterator {
2187
2187
. map ( |first| once ( first) . chain ( self ) . product ( ) )
2188
2188
}
2189
2189
2190
+ /// Sort all iterator elements into a new iterator in ascending order.
2191
+ ///
2192
+ /// **Note:** This consumes the entire iterator, uses the
2193
+ /// `slice::sort_unstable()` method and returns the result as a new
2194
+ /// iterator that owns its elements.
2195
+ ///
2196
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
2197
+ /// without any extra copying or allocation cost.
2198
+ ///
2199
+ /// ```
2200
+ /// use itertools::Itertools;
2201
+ ///
2202
+ /// // sort the letters of the text in ascending order
2203
+ /// let text = "bdacfe";
2204
+ /// itertools::assert_equal(text.chars().sorted_unstable(),
2205
+ /// "abcdef".chars());
2206
+ /// ```
2207
+ #[ cfg( feature = "use_alloc" ) ]
2208
+ fn sorted_unstable ( self ) -> VecIntoIter < Self :: Item >
2209
+ where Self : Sized ,
2210
+ Self :: Item : Ord
2211
+ {
2212
+ // Use .sort_unstable() directly since it is not quite identical with
2213
+ // .sort_by(Ord::cmp)
2214
+ let mut v = Vec :: from_iter ( self ) ;
2215
+ v. sort_unstable ( ) ;
2216
+ v. into_iter ( )
2217
+ }
2218
+
2219
+ /// Sort all iterator elements into a new iterator in ascending order.
2220
+ ///
2221
+ /// **Note:** This consumes the entire iterator, uses the
2222
+ /// `slice::sort_unstable_by()` method and returns the result as a new
2223
+ /// iterator that owns its elements.
2224
+ ///
2225
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
2226
+ /// without any extra copying or allocation cost.
2227
+ ///
2228
+ /// ```
2229
+ /// use itertools::Itertools;
2230
+ ///
2231
+ /// // sort people in descending order by age
2232
+ /// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)];
2233
+ ///
2234
+ /// let oldest_people_first = people
2235
+ /// .into_iter()
2236
+ /// .sorted_unstable_by(|a, b| Ord::cmp(&b.1, &a.1))
2237
+ /// .map(|(person, _age)| person);
2238
+ ///
2239
+ /// itertools::assert_equal(oldest_people_first,
2240
+ /// vec!["Jill", "Jack", "Jane", "John"]);
2241
+ /// ```
2242
+ #[ cfg( feature = "use_alloc" ) ]
2243
+ fn sorted_unstable_by < F > ( self , cmp : F ) -> VecIntoIter < Self :: Item >
2244
+ where Self : Sized ,
2245
+ F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
2246
+ {
2247
+ let mut v = Vec :: from_iter ( self ) ;
2248
+ v. sort_unstable_by ( cmp) ;
2249
+ v. into_iter ( )
2250
+ }
2251
+
2252
+ /// Sort all iterator elements into a new iterator in ascending order.
2253
+ ///
2254
+ /// **Note:** This consumes the entire iterator, uses the
2255
+ /// `slice::sort_unstable_by_key()` method and returns the result as a new
2256
+ /// iterator that owns its elements.
2257
+ ///
2258
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
2259
+ /// without any extra copying or allocation cost.
2260
+ ///
2261
+ /// ```
2262
+ /// use itertools::Itertools;
2263
+ ///
2264
+ /// // sort people in descending order by age
2265
+ /// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)];
2266
+ ///
2267
+ /// let oldest_people_first = people
2268
+ /// .into_iter()
2269
+ /// .sorted_unstable_by_key(|x| -x.1)
2270
+ /// .map(|(person, _age)| person);
2271
+ ///
2272
+ /// itertools::assert_equal(oldest_people_first,
2273
+ /// vec!["Jill", "Jack", "Jane", "John"]);
2274
+ /// ```
2275
+ #[ cfg( feature = "use_alloc" ) ]
2276
+ fn sorted_unstable_by_key < K , F > ( self , f : F ) -> VecIntoIter < Self :: Item >
2277
+ where Self : Sized ,
2278
+ K : Ord ,
2279
+ F : FnMut ( & Self :: Item ) -> K ,
2280
+ {
2281
+ let mut v = Vec :: from_iter ( self ) ;
2282
+ v. sort_unstable_by_key ( f) ;
2283
+ v. into_iter ( )
2284
+ }
2190
2285
2191
2286
/// Sort all iterator elements into a new iterator in ascending order.
2192
2287
///
0 commit comments