Page 1 of 1

usort - example?

Posted: Mon Dec 15, 2008 5:03 am
by matstars
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have an array, $employee, that outputs as such

Code: Select all

[1] => Array
(
[name] => Michael Smith
[age] => 34
[experience] => 4.3
)
 
 [2] => Array
(
[name] => Brandon Singh
[age] => 28
[experience] => 7.4
)
 
[3]
(
[name] => Robert Sant
[age] => 40
[experience] => 17.3
)
I am trying to sort by Experience ascending and descending - I tried the following, but it doesn't work, can anyone help?

Code: Select all

function columnSort($unsorted, $column) {
    $sorted = $unsorted;
    for ($i=0; $i < sizeof($sorted)-1; $i++) {
      for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
        if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
          $tmp = $sorted[$j];
          $sorted[$j] = $sorted[$j+1];
          $sorted[$j+1] = $tmp;
      }
    }
    return $sorted;
}
    
function columnSortReverse($unsorted, $column) {
    $sorted = $unsorted;
    for ($i=0; $i < sizeof($sorted)-1; $i++) {
      for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
        if ($sorted[$j][$column] < $sorted[$j+1][$column]) {
          $tmp = $sorted[$j];
          $sorted[$j] = $sorted[$j+1];
          $sorted[$j+1] = $tmp;
      }
    }
    return $sorted;
}
    
$employee["name"]="Michael Smith";
$employee["age"]=34;
$employee["experience"]=4.3;
 
$employee=array(
    array(Name=>"Michael Smith", Age=>34, Exp=>4.3),
    array(Name=>"Brandon Singh", Age=>28, Exp=>7.4),
    array(Name=>"Robert Sant", Age=>40, Exp=>17.3)
    );
    
echo var_dump($employee);
echo "<BR>";
print_r(columnSort($employee, 'Experience'));
echo "<BR><BR>";
print_r(columnSortReverse($employee, 'Experience'));
echo "<BR><BR>";


Thanks,

Mat


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: usort - example?

Posted: Mon Dec 15, 2008 6:40 am
by novice4eva

Code: Select all

 
function columnSort($unsorted,$sortOrder,$sortCol) 
{
 
    $tmpArray = array();
    $arrayLen = count($unsorted);
    for ($i=0; $i < $arrayLen; $i++) 
    {
        $tmpMax = $unsorted[$i];
        for ($j=$i+1; $j<$arrayLen; $j++)
        {
            if($sortOrder=='D')
            {
                if($unsorted[$j][$sortCol]>$unsorted[$i][$sortCol])
                {
                    $tmpArray = $unsorted[$j];
                    $unsorted[$j] = $unsorted[$i];
                    $unsorted[$i] = $tmpArray;
                }
            }
            else
            {
                if($unsorted[$j][$sortCol]<$unsorted[$i][$sortCol])
                {
                    $tmpArray = $unsorted[$j];
                    $unsorted[$j] = $unsorted[$i];
                    $unsorted[$i] = $tmpArray;
                }
            }
        }
    }
    return $unsorted;
}
 
$employee=array(
array('Name'=>"Michael Smith", 'Age'=>34, 'Exp'=>4.3),
array('Name'=>"Brandon Singh", 'Age'=>28, 'Exp'=>7.4),
array('Name'=>"Robert Sant", 'Age'=>40, 'Exp'=>17.3)
);
echo '<pre>';
print_r(columnSort($employee,'A','Name'));
echo '</pre>';
 
This very much brought back the college C practical days :D

Re: usort - example?

Posted: Mon Dec 15, 2008 10:43 am
by pickle
Why not save yourself some time & actually use usort() like your title suggests? You can use array_reverse() to handle your reverse sorting requirement, rather than writing a whole new algorithm. array_multisort() (if you know how to use it) is probably your best bet here.