@@ -1540,11 +1540,12 @@ pub mod array_functions {
15401540 /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
15411541 ///
15421542 /// // Do comparisons in reverse
1543- /// x.sort (|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
1543+ /// x.sort_by (|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
15441544 ///
15451545 /// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
15461546 /// ```
1547- pub fn sort ( ctx : NativeCallContext , array : & mut Array , comparer : FnPtr ) {
1547+ #[ rhai_fn( name = "sort" , name = "sort_by" ) ]
1548+ pub fn sort_by ( ctx : NativeCallContext , array : & mut Array , comparer : FnPtr ) {
15481549 if array. len ( ) <= 1 {
15491550 return ;
15501551 }
@@ -1664,6 +1665,220 @@ pub mod array_functions {
16641665
16651666 Ok ( ( ) )
16661667 }
1668+ /// Sort the array in descending order.
1669+ ///
1670+ /// All elements in the array must be of the same data type.
1671+ ///
1672+ /// # Supported Data Types
1673+ ///
1674+ /// * integer numbers
1675+ /// * floating-point numbers
1676+ /// * decimal numbers
1677+ /// * characters
1678+ /// * strings
1679+ /// * booleans
1680+ /// * `()`
1681+ ///
1682+ /// # Example
1683+ ///
1684+ /// ```rhai
1685+ /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1686+ ///
1687+ /// x.sort_desc();
1688+ ///
1689+ /// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1690+ /// ```
1691+ #[ rhai_fn( name = "sort_desc" , return_raw) ]
1692+ pub fn sort_with_builtin_desc ( array : & mut Array ) -> RhaiResultOf < ( ) > {
1693+ if array. len ( ) <= 1 {
1694+ return Ok ( ( ) ) ;
1695+ }
1696+
1697+ let type_id = array[ 0 ] . type_id ( ) ;
1698+
1699+ if array. iter ( ) . any ( |a| a. type_id ( ) != type_id) {
1700+ return Err ( ERR :: ErrorFunctionNotFound (
1701+ "sort_desc() cannot be called with elements of different types" . into ( ) ,
1702+ Position :: NONE ,
1703+ )
1704+ . into ( ) ) ;
1705+ }
1706+
1707+ if type_id == TypeId :: of :: < INT > ( ) {
1708+ array. sort_by ( |a, b| {
1709+ let a = a. as_int ( ) . unwrap ( ) ;
1710+ let b = b. as_int ( ) . unwrap ( ) ;
1711+ match a. cmp ( & b) {
1712+ Ordering :: Less => Ordering :: Greater ,
1713+ Ordering :: Equal => Ordering :: Equal ,
1714+ Ordering :: Greater => Ordering :: Less ,
1715+ }
1716+ } ) ;
1717+ return Ok ( ( ) ) ;
1718+ }
1719+ if type_id == TypeId :: of :: < char > ( ) {
1720+ array. sort_by ( |a, b| {
1721+ let a = a. as_char ( ) . unwrap ( ) ;
1722+ let b = b. as_char ( ) . unwrap ( ) ;
1723+ match a. cmp ( & b) {
1724+ Ordering :: Less => Ordering :: Greater ,
1725+ Ordering :: Equal => Ordering :: Equal ,
1726+ Ordering :: Greater => Ordering :: Less ,
1727+ }
1728+ } ) ;
1729+ return Ok ( ( ) ) ;
1730+ }
1731+ #[ cfg( not( feature = "no_float" ) ) ]
1732+ if type_id == TypeId :: of :: < crate :: FLOAT > ( ) {
1733+ array. sort_by ( |a, b| {
1734+ let a = a. as_float ( ) . unwrap ( ) ;
1735+ let b = b. as_float ( ) . unwrap ( ) ;
1736+ match a. partial_cmp ( & b) . unwrap_or ( Ordering :: Equal ) {
1737+ Ordering :: Less => Ordering :: Greater ,
1738+ Ordering :: Equal => Ordering :: Equal ,
1739+ Ordering :: Greater => Ordering :: Less ,
1740+ }
1741+ } ) ;
1742+ return Ok ( ( ) ) ;
1743+ }
1744+ if type_id == TypeId :: of :: < ImmutableString > ( ) {
1745+ array. sort_by ( |a, b| {
1746+ let a = & * a. as_immutable_string_ref ( ) . unwrap ( ) ;
1747+ let b = & * b. as_immutable_string_ref ( ) . unwrap ( ) ;
1748+ match a. cmp ( & b) {
1749+ Ordering :: Less => Ordering :: Greater ,
1750+ Ordering :: Equal => Ordering :: Equal ,
1751+ Ordering :: Greater => Ordering :: Less ,
1752+ }
1753+ } ) ;
1754+ return Ok ( ( ) ) ;
1755+ }
1756+ #[ cfg( feature = "decimal" ) ]
1757+ if type_id == TypeId :: of :: < rust_decimal:: Decimal > ( ) {
1758+ array. sort_by ( |a, b| {
1759+ let a = a. as_decimal ( ) . unwrap ( ) ;
1760+ let b = b. as_decimal ( ) . unwrap ( ) ;
1761+ match a. cmp ( & b) {
1762+ Ordering :: Less => Ordering :: Greater ,
1763+ Ordering :: Equal => Ordering :: Equal ,
1764+ Ordering :: Greater => Ordering :: Less ,
1765+ }
1766+ } ) ;
1767+ return Ok ( ( ) ) ;
1768+ }
1769+ if type_id == TypeId :: of :: < bool > ( ) {
1770+ array. sort_by ( |a, b| {
1771+ let a = a. as_bool ( ) . unwrap ( ) ;
1772+ let b = b. as_bool ( ) . unwrap ( ) ;
1773+ match a. cmp ( & b) {
1774+ Ordering :: Less => Ordering :: Greater ,
1775+ Ordering :: Equal => Ordering :: Equal ,
1776+ Ordering :: Greater => Ordering :: Less ,
1777+ }
1778+ } ) ;
1779+ return Ok ( ( ) ) ;
1780+ }
1781+ if type_id == TypeId :: of :: < ( ) > ( ) {
1782+ return Ok ( ( ) ) ;
1783+ }
1784+
1785+ Ok ( ( ) )
1786+ }
1787+ /// Sort the array based on applying the `comparer` function and return it as a new array.
1788+ ///
1789+ /// # Function Parameters
1790+ ///
1791+ /// * `element1`: copy of the current array element to compare
1792+ /// * `element2`: copy of the next array element to compare
1793+ ///
1794+ /// ## Return Value
1795+ ///
1796+ /// An integer number:
1797+ ///
1798+ /// * Any positive integer if `element1 > element2`
1799+ /// * 0 if `element1 == element2`
1800+ /// * Any negative integer if `element1 < element2`
1801+ ///
1802+ /// or a boolean value:
1803+ ///
1804+ /// * `true` if `element1 <= element2`
1805+ /// * `false` if `element1 > element2`
1806+ ///
1807+ /// Any other return value type will yield unpredictable order.
1808+ ///
1809+ /// # Example
1810+ ///
1811+ /// ```rhai
1812+ /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1813+ ///
1814+ /// // Do comparisons in reverse
1815+ /// let y = x.order_by(|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
1816+ ///
1817+ /// print(y); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1818+ /// ```
1819+ pub fn order_by ( ctx : NativeCallContext , array : & mut Array , comparer : FnPtr ) -> Array {
1820+ let mut array = array. clone ( ) ;
1821+ sort_by ( ctx, & mut array, comparer) ;
1822+ array
1823+ }
1824+ /// Sort the array and return it as a new array.
1825+ ///
1826+ /// All elements in the array must be of the same data type.
1827+ ///
1828+ /// # Supported Data Types
1829+ ///
1830+ /// * integer numbers
1831+ /// * floating-point numbers
1832+ /// * decimal numbers
1833+ /// * characters
1834+ /// * strings
1835+ /// * booleans
1836+ /// * `()`
1837+ ///
1838+ /// # Example
1839+ ///
1840+ /// ```rhai
1841+ /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1842+ ///
1843+ /// let y = x.order();
1844+ ///
1845+ /// print(y); // prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
1846+ /// ```
1847+ #[ rhai_fn( name = "order" , return_raw) ]
1848+ pub fn order_with_builtin ( array : & mut Array ) -> RhaiResultOf < Array > {
1849+ let mut array = array. clone ( ) ;
1850+ sort_with_builtin ( & mut array) ?;
1851+ Ok ( array)
1852+ }
1853+ /// Sort the array in descending order and return it as a new array.
1854+ ///
1855+ /// All elements in the array must be of the same data type.
1856+ ///
1857+ /// # Supported Data Types
1858+ ///
1859+ /// * integer numbers
1860+ /// * floating-point numbers
1861+ /// * decimal numbers
1862+ /// * characters
1863+ /// * strings
1864+ /// * booleans
1865+ /// * `()`
1866+ ///
1867+ /// # Example
1868+ ///
1869+ /// ```rhai
1870+ /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1871+ ///
1872+ /// let y = x.order_desc();
1873+ ///
1874+ /// print(y); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1875+ /// ```
1876+ #[ rhai_fn( name = "order_desc" , return_raw) ]
1877+ pub fn order_with_builtin_desc ( array : & mut Array ) -> RhaiResultOf < Array > {
1878+ let mut array = array. clone ( ) ;
1879+ sort_with_builtin_desc ( & mut array) ?;
1880+ Ok ( array)
1881+ }
16671882 /// Remove all elements in the array that returns `true` when applied the `filter` function and
16681883 /// return them as a new array.
16691884 ///
0 commit comments