Function to sort an array and return the sorted array
Asked Answered
R

4

24

PHP's native sorting functions modify by reference and do not return the sorted array.

I am looking for a reliable standard method to sort an array, returning the sorted array as the return value.

All of the PHP.net functions I have read about return BOOLEAN value, or 0-1.

The method I need would be something like:

$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{ 
  // Sort it
  // return sorted array
}

custom_sort( $some_mixed_array );

// returning: array( 6, 430, 998 )

No need to handle strings, just INT-s.

Rennarennane answered 10/9, 2013 at 13:45 Comment(7)
Why not sort a copy of the array using PHP functions and return the result?Charlyncharm
Can the original array be modified or should it be left as is? If not, function custom_sort($a) { sort($a); return $a; }Espouse
function custom_short($array) { asort($array); return $array } this one?Conference
do you mean 'short' or 'sort'? In your code you have shortContinuant
While the sort functions return a bool they still sort your array - so what problem are you trying to solve ?Marhtamari
The sort(), asort(), ksort(), etc functions actually alter the array that you pass to them, so you can simply return the exact same array after it's been sorted.Beloved
Why so many downvotes?! It seems like an excellent question, that has a nice clear answer. As a reason to want this, you have one-liners like: assert(sort(array_keys($myData)) == array('x','y')) This fails with "Only variables should be passed by reference".Weigand
S
13

Here's a one-liner:

call_user_func(function(array $a){asort($a);return $a;}, $some_mixed_array);

Storage answered 3/5, 2018 at 15:39 Comment(0)
B
5

Would you be able to do this?

$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{
  // Sort it
  asort($array);

  // return sorted array
  return $array;
}

custom_sort( $some_mixed_array );

// returning: array( 6, 430, 998 )

This would also solve your issue:

$some_mixed_array = array( 998, 6, 430 );
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';

asort($some_mixed_array); // <- BAM!

// returning: array( 6, 430, 998 )
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';
Beloved answered 10/9, 2013 at 13:52 Comment(2)
You should warn that these two solutions do two different things. The first one doesn't change the original array, while the second does. Plus, using asort on number-indexed array is pretty weird. You maintain the original keys, but you are unable to iterate it naturally. It loses its nature of pure array.Bemba
sorted is the name of the function python usesStinger
O
4

As others have said, your best bet is to create a custom function. However, in order to maintain flexibility with the future of PHP, I would use a variadic function. Essentially, you set your function to accepts whatever parameters are passed to it, and pass them through to the actual sort() function. Done this way, you can use whatever optional parameters exist for the standard function you're putting the wrapper around -- even if those parameters change in the future.

function mysort( ...$params ) {
    sort( ...$params );
    return $params[0];
}

UPDATE: Same functionality, but somewhat easier to understand:

function mysort( $array, ...$params ) {
    sort( $array, ...$params );
    return $array;
}
Outcrop answered 3/10, 2019 at 19:17 Comment(0)
H
0

From PHP7, you can feed your input array to an IIFE if you like one-liners. This will not mutate the original array, but return the mutated version of a copy of the original.

Code: (Demo)

$some_mixed_array = [998, 6, 430];

var_export(
    (function($v) { sort($v); return $v; })($some_mixed_array)
);
  • (function($v) { sort($v); return $v; }) is the closure.
  • ($some_mixed_array) is the single parameter inside the function signature.

sort() will not preserve the array's keys after sorting.
asort() will preserve the array's keys after sorting.


Of if you prefer brevity and don't mind using a ternary condition, you can enjoy PHP's arrow function syntax. (Demo)

var_export(
    (fn($v) => sort($v) ? $v : $v)($some_mixed_array)
);
Heatherheatherly answered 30/1, 2023 at 22:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.