usort - example?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matstars
Forum Newbie
Posts: 7
Joined: Tue Aug 12, 2008 2:47 pm

usort - example?

Post 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.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: usort - example?

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: usort - example?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply