@@ -2667,6 +2667,43 @@ pub trait Itertools : Iterator {
2667
2667
v. into_iter ( )
2668
2668
}
2669
2669
2670
+ /// Sort all iterator elements into a new iterator in ascending order. The key function is
2671
+ /// called exactly once per key.
2672
+ ///
2673
+ /// **Note:** This consumes the entire iterator, uses the
2674
+ /// [`slice::sort_by_cached_key`] method and returns the result as a new
2675
+ /// iterator that owns its elements.
2676
+ ///
2677
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
2678
+ /// without any extra copying or allocation cost.
2679
+ ///
2680
+ /// ```
2681
+ /// use itertools::Itertools;
2682
+ ///
2683
+ /// // sort people in descending order by age
2684
+ /// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)];
2685
+ ///
2686
+ /// let oldest_people_first = people
2687
+ /// .into_iter()
2688
+ /// .sorted_by_cached_key(|x| -x.1)
2689
+ /// .map(|(person, _age)| person);
2690
+ ///
2691
+ /// itertools::assert_equal(oldest_people_first,
2692
+ /// vec!["Jill", "Jack", "Jane", "John"]);
2693
+ /// ```
2694
+ /// ```
2695
+ #[ cfg( feature = "use_alloc" ) ]
2696
+ fn sorted_by_cached_key < K , F > ( self , f : F ) -> VecIntoIter < Self :: Item >
2697
+ where
2698
+ Self : Sized ,
2699
+ K : Ord ,
2700
+ F : FnMut ( & Self :: Item ) -> K ,
2701
+ {
2702
+ let mut v = Vec :: from_iter ( self ) ;
2703
+ v. sort_by_cached_key ( f) ;
2704
+ v. into_iter ( )
2705
+ }
2706
+
2670
2707
/// Sort the k smallest elements into a new iterator, in ascending order.
2671
2708
///
2672
2709
/// **Note:** This consumes the entire iterator, and returns the result
0 commit comments