25

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.

7
  • Why not sort a copy of the array using PHP functions and return the result? Commented Sep 10, 2013 at 13:47
  • Can the original array be modified or should it be left as is? If not, function custom_sort($a) { sort($a); return $a; } Commented Sep 10, 2013 at 13:47
  • 1
    do you mean 'short' or 'sort'? In your code you have short Commented Sep 10, 2013 at 13:47
  • 2
    While the sort functions return a bool they still sort your array - so what problem are you trying to solve ? Commented Sep 10, 2013 at 13:47
  • 9
    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". Commented Jan 22, 2014 at 1:31

4 Answers 4

13

Here's a one-liner:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Why is a one-liner a good thing? This only makes readability a bit harder, in my view. Also, it would be great to explain why this works. I know why it works but you could have explained it. For example, why don't we need to use say clone? Many people will just copy and paste this code and will not even understand it.
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>';

2 Comments

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.
sorted is the name of the function python uses
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;
}

Comments

1

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)
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.