phparrayssortingassociative-arraycustom-function

Function to sort an array and return the sorted array


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.


Solution

  • Here's a one-liner:

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