Jump to content

Array Multisort Question


TFT2012

Recommended Posts

Hi all,

I have a question regarding array_multisort(). For instance, I have one array as follow:

 

$arr = array( 
	array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ),
	array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ),
	array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ),
	array("PSize" => 396597, "DSize" => 8, "CFSize" => 101311 )
);

 

In HTML, I have multiple selection boxes to select columns and types to sort. Each column will have one selection box like this.

<select id='selectorId'>
<option value='none'>None</option>
<option value='SORT_ASC'>Ascending</option>
<option value='SORT_DESC'>Descending</option>
</select>

 

When submit, I will return the string like "~column_1|sorting_type_1~column_2|sorting_type_2~...." back to AJax.

 

If user selects "none", that means NO SORT to this column. Then I want:

For example, if "PSize" is set to "none", the other two columns are set to "SORT_ASC", I can do

foreach( $arr as $key => $row )
{
//Only get the columns user want to sort.
        $sort_dsize[] = $row["DSize"];
$sort_cfsize[] = $row["CFSize"];
}
array_multisort($sort_dsize, SORT_ASC, $sort_cfsize, SORT_ASC, $arr);

 

If user only want to sort "PSize", then

foreach( $arr as $key => $row )
{
//Only get the columns user want to sort.
        $sort_psize[] = $row["PSize"];
}
array_multisort($sort_psize, SORT_ASC, $arr);

 

How can I do this dynamically? Not to use swtich, case or if/else.

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/263767-array-multisort-question/
Share on other sites

Custom sort on multiple columns.

 

Note that in your params I have used 0 for none, 1 for ASC and -1 for DESC sort on that column (easy then to just mutiply result of comparison)

 

<?php

$paramStr = "Psize|0~DSize|-1~CFSize|1";            // none = 0,  ASC = 1,   DESC = -1

$parr = explode('~', $paramStr);
foreach ($parr as $p) {
    $params[] = explode('|', $p);
}

function mysort ($a, $b)
{
    global $params;
    
    $x1 = ($a[$params[0][0]] - $b[$params[0][0]]) * $params[0][1];
    if ($x1==0) {
        $x2 = ($a[$params[1][0]] - $b[$params[1][0]]) * $params[1][1];
        if ($x2==0) {
            return ($a[$params[2][0]] - $b[$params[2][0]]) * $params[2][1];
        }
        else return $x2;
    }
    else return $x1;
}

$arr = array( 
        array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ),
        array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ),
        array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ),
        array("PSize" => 396597, "DSize" => 5, "CFSize" => 101311 )
    );

uasort($arr, 'mysort');

echo '<pre>'.print_r($arr, 1).'</pre>';
?> 

  • 3 weeks later...

Thanks for the solution.  :)

 

Custom sort on multiple columns.

 

Note that in your params I have used 0 for none, 1 for ASC and -1 for DESC sort on that column (easy then to just mutiply result of comparison)

 

<?php

$paramStr = "Psize|0~DSize|-1~CFSize|1";            // none = 0,  ASC = 1,   DESC = -1

$parr = explode('~', $paramStr);
foreach ($parr as $p) {
    $params[] = explode('|', $p);
}

function mysort ($a, $b)
{
    global $params;
    
    $x1 = ($a[$params[0][0]] - $b[$params[0][0]]) * $params[0][1];
    if ($x1==0) {
        $x2 = ($a[$params[1][0]] - $b[$params[1][0]]) * $params[1][1];
        if ($x2==0) {
            return ($a[$params[2][0]] - $b[$params[2][0]]) * $params[2][1];
        }
        else return $x2;
    }
    else return $x1;
}

$arr = array( 
        array("PSize" => 216597, "DSize" => 1, "CFSize" => 172504 ),
        array("PSize" => 106597, "DSize" => 10, "CFSize" => 61311 ),
        array("PSize" => 166597, "DSize" => 5, "CFSize" => 261311 ),
        array("PSize" => 396597, "DSize" => 5, "CFSize" => 101311 )
    );

uasort($arr, 'mysort');

echo '<pre>'.print_r($arr, 1).'</pre>';
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.