@@ -651,7 +651,7 @@ impl<T: ArrayElement> Array<T> {
651
651
let ignored_value = <T as ParamType >:: owned_to_arg ( ignored_value) ;
652
652
653
653
let godot_comparator = |args : & [ & Variant ] | {
654
- let value = T :: from_variant ( & args[ 0 ] ) ;
654
+ let value = T :: from_variant ( args[ 0 ] ) ;
655
655
let is_less = matches ! ( func( & value) , cmp:: Ordering :: Less ) ;
656
656
657
657
Ok ( is_less. to_variant ( ) )
@@ -697,20 +697,55 @@ impl<T: ArrayElement> Array<T> {
697
697
698
698
/// Sorts the array.
699
699
///
700
- /// Note: The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
701
- /// This means that values considered equal may have their order changed when using `sort_unstable`.
700
+ /// The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
701
+ /// This means that values considered equal may have their order changed when using `sort_unstable`. For most variant types,
702
+ /// this distinction should not matter though.
703
+ ///
704
+ /// _Godot equivalent: `Array.sort()`_
702
705
#[ doc( alias = "sort" ) ]
703
706
pub fn sort_unstable ( & mut self ) {
704
707
// SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
705
708
unsafe { self . as_inner_mut ( ) } . sort ( ) ;
706
709
}
707
710
708
- /// Sorts the array.
711
+ /// Sorts the array, using a type-safe comparator.
712
+ ///
713
+ /// The predicate expects two parameters `(a, b)` and should return an ordering relation. For example, simple ascending ordering of the
714
+ /// elements themselves would be achieved with `|a, b| a.cmp(b)`.
715
+ ///
716
+ /// The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
717
+ /// This means that values considered equal may have their order changed when using `sort_unstable_by`. For most variant types,
718
+ /// this distinction should not matter though.
719
+ #[ cfg( since_api = "4.2" ) ]
720
+ pub fn sort_unstable_by < F > ( & mut self , mut func : F )
721
+ where
722
+ F : FnMut ( & T , & T ) -> cmp:: Ordering ,
723
+ {
724
+ let godot_comparator = |args : & [ & Variant ] | {
725
+ let lhs = T :: from_variant ( args[ 0 ] ) ;
726
+ let rhs = T :: from_variant ( args[ 1 ] ) ;
727
+ let is_less = matches ! ( func( & lhs, & rhs) , cmp:: Ordering :: Less ) ;
728
+
729
+ Ok ( is_less. to_variant ( ) )
730
+ } ;
731
+
732
+ let debug_name = std:: any:: type_name :: < F > ( ) ;
733
+ Callable :: with_scoped_fn ( debug_name, godot_comparator, |pred| {
734
+ self . sort_unstable_custom ( pred)
735
+ } ) ;
736
+ }
737
+
738
+ /// Sorts the array, using type-unsafe `Callable` comparator.
739
+ ///
740
+ /// For a type-safe variant of this method, use [`sort_unstable_by()`][Self::sort_unstable_by].
741
+ ///
742
+ /// The callable expects two parameters `(lhs, rhs)` and should return a bool `lhs < rhs`.
709
743
///
710
- /// Uses the provided `Callable` to determine ordering.
744
+ /// The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
745
+ /// This means that values considered equal may have their order changed when using `sort_unstable_custom`.For most variant types,
746
+ /// this distinction should not matter though.
711
747
///
712
- /// Note: The sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
713
- /// This means that values considered equal may have their order changed when using `sort_unstable_custom`.
748
+ /// _Godot equivalent: `Array.sort_custom()`_
714
749
#[ doc( alias = "sort_custom" ) ]
715
750
pub fn sort_unstable_custom ( & mut self , func : & Callable ) {
716
751
// SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
0 commit comments