0

I have the problem with sort direction. I try to sort multi-dimensional array with direction. I can't use array_multisort() directly, because I don't know how many parametrs will be. I use call_user_func_array('array_multisort', $params); And it works, but I can't set sort direction (SORT_ASC,SORT_DESC). How can I set sort direction for call_user_func_array('array_multisort', $params);? Here is my code, you can try it

function get_fields($data, $order_by) {
    $order_row = preg_split("/[\s,]+/", $order_by);
    for ($i=0;$i<count($order_row);$i++) {
        foreach ($data as $key => $row) {
          $tmp[$i][$key]  = $row[$order_row[$i]];      
        }
    }
    return $tmp;
}

function ordering($data, $order_by) {
    $tmp = get_fields($data, $order_by);
    $params = array();
    foreach($tmp as &$t){
        $params[] = &$t;
    }

    $params[1] = array("SORT_DESC","SORT_DESC","SORT_DESC","SORT_DESC"); // like that no warning but no sorting

    $params[] = &$data;
    call_user_func_array('array_multisort', $params);
    return array_pop($params);
}

$data = array (
    array('id' => 1,'name' => 'Barack','city' => 9),
    array('id' => 7,'name' => 'boris','city' => 2),
    array('id' => 3,'name' => 'coris','city' => 2),
    array('id' => 3,'name' => 'coris','city' => 2)
);

$order_by = "city desc, name";

echo "<br>ORDER BY $order_by<br>";
$ordered = ordering($data, $order_by);
echo "<pre>";
var_dump($ordered);
echo "</pre>";

I want to do a sort like MySQL ORDER BY city DESC, name. It's my goal.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
ksetrin
  • 13
  • 6

3 Answers3

1

To be able to sort an array multiple times and achieve a result like ORDER BY city DESC, name ASC you need a function that does a stable sort.
As far as I know PHP doesn't have one so you have to sort it once with a comparator function like this

$data = array (
    array('id' => 3,'name' => 'coris','city' => 2),
    array('id' => 1,'name' => 'Barack','city' => 9),
    array('id' => 7,'name' => 'boris','city' => 2),
    array('id' => 3,'name' => 'coris','city' => 2),
);

$order_by = array(
    'city' => array('dir' => SORT_DESC, 'type' => SORT_NUMERIC),
    'name' => array('dir' => SORT_ASC, 'type' => SORT_STRING),
);

function compare($row1,$row2) {
    /* this function should determine which row is greater based on all of the criteria
       and return a negative number when $row1 < $row2
                  a positive number when $row1 > $row2
                  0 when $row1 == $row2
    */

    global $order_by;

    foreach($order_by as $field => $sort) {
        if($sort['type'] != SORT_NUMERIC) {
            // strings are compared case insensitive and assumed to be in the mb_internal_encoding
            $cmp = strcmp(mb_strtolower($row1[$field]), mb_strtolower($row2[$field]));
        } else {
            $cmp = doubleval($row1[$field]) - doubleval($row2[$field]);
        }
        if($sort['dir'] != SORT_ASC) $cmp = -$cmp;
        if($cmp != 0) return $cmp;
    }
    return 0;
}


usort($data,'compare');
Vatev
  • 7,493
  • 1
  • 32
  • 39
0

I had the same problem. It seems that call_user_func_array() can't handle the constants. I've solved this problem by dynamically building an argument string and evaluating this string:

$args  = array($arr1, $arr2);
$order = array(SORT_ASC, SORT_DESC);
$evalstring = '';

foreach($args as $i=>$arg){
    if($evalstring == ''){ $evalstring.= ', '; }
    $evalstring.= '$arg';
    $evalstring.= ', '.$order[$i];
}
eval("array_multisort($evalstring);");

I know eval() is evil and this is not a clean way, but it works ;-)

Marc
  • 585
  • 5
  • 19
-1

It is working for me :

$arrayThatNeedToSort = array('data..');
$multiSortprop = array(['data.....']=> SORT_DESC,['data.....'] => SORT_ASC)

$properties = array();
foreach ($multiSortprop as $sortArr => $sortArg) {
         array_push($properties,$sortArr);
         array_push($properties,$sortArg);
 }

array_push($properties,$arrayThatNeedToSort);
array_multisort(...$properties);

var_dump(end($properties));
Stepan
  • 1