2

Why array_multisort doesn't sort in my case ?

The case is simple i followed many of examples manuals and i have no idea why it doesnt work. Teoretical it should work 100%. Maybe i have spend to many time trying to fix it over and over again and i cant see samething obvious now.

Here is the case:

I want to sort my array $this->array over defined sort dirrection for columns in other array $order

simple sorting function:

$preSort = [];
        foreach ($order as $column => $direction) {
            $valueArray = [];
            foreach ($this->array as $key => $row) {
                $valueArray[] = $row[$column];
            }
            $preSort[] = $valueArray;
            $preSort[] = $this->getSortDirection($direction) | $this->getSortType(key($valueArray), $column);
        }
        $preSort[] = $this->array;
call_user_func_array('array_multisort', $preSort);

And here it is how data looks like: $order

array ('id' => 'asc', 'name' => 'desc',)

$this->array

array ( 0 => array ( 'id' => 1, 'name' => 'samsonasik', 'description' => 'aaaaaaa', ), 1 => array ( 'id' => 2, 'name' => 'abdul malik ikhsan', 'description' => 'bbbbbb', ), 2 => array ( 'id' => 3, 'name' => 'superman', 'description' => 'cccccccc', ), )

here you have extra functions used by me:

protected function getSortDirection($string)
{
    return mb_strtolower($string) == 'asc' ? SORT_ASC : SORT_DESC;
}

protected function getSortType($key, $column)
{
    return is_numeric($this->array[$key][$column]) ? SORT_NUMERIC : SORT_STRING;
}
vardius
  • 6,326
  • 8
  • 52
  • 97
  • Please, show input data and desired output data using `var_export`. – sectus Dec 12 '14 at 09:16
  • i edited the `$order` and `$this->array` to `var_export` order aray contains the `keys` which i want to use when sorting using array_multisort function http://php.net/manual/en/function.array-multisort.php. i dont know the keys and order (desc or asc) of those filds becouse its defined by user. and it can be multiple as you can see. i use `call_user_func_array` to invoke `array_multisort`. why ? becouse i can pass the parameters in array, and i build the array with parameters and then pass it. – vardius Dec 12 '14 at 15:27
  • `$preSort` is an array containing parameters: `(column_array, flags, array_to_sort)` where if there is multiple column to sort it looks like: `(column_array, flags, column_array, flags, array_to_sort)` and goone for more ... `array_to_sort` is always as element of `preSort` array. there are always like elements set like first column then order and next the other column its order flag and as many columns to sort by we have to alst the array to sort – vardius Dec 12 '14 at 15:33

1 Answers1

0

the array_multisort signature looks like this:

bool array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC
    [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )

$sort_order and $sort_flags are two parameters, so instead of

$array, SORT_ASC | SORT_STRING

you need

$array, SORT_ASC, SORT_STRING
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111