[eluser]elaniobro[/eluser]
[quote author="Kamarg" date="1268099150"]You just about had it. Put a & in front of $input in your function declaration so that _osort can modify the contents of $input. Then you just need to call _osort somewhere passing $input as the first parameter and 'id' or whatever object member you want to sort on.
As far as the overall code goes, the _osort function takes an array by reference (so that changes persist when the function returns) which is indicated by the & and a string of the index to sort the array on. It then calls usort passing the array to sort as the first parameter. For the second parameter it creates an anonymous function with two parameters ($a and $b) with the function body being the text. The anonymous function body is just a simple test to see if the value of a specific object member is equal, in which case it returns zero or returns 1 or -1 depending on if $a or $b is larger.
I'm not very good at explaining but I hope that helps.[/quote]
This does make sense, more so then just looking at the code, thanks for the explanation. As for the implemenatatin I put the following in my view file, while no errors are happening, the sorting by the 'id' is not happening.
Code:
<?php
#print_r ($query->result());
$input = $query->result();
$result= array_reverse($input);
function _osort(&$input, $p) {
usort($input, create_function('$a,$b', 'if($a->' . $p . ' == $b->' . $p . ') { return 0; } else { return ($a->' . $p . ' > $b->' . $p . ' ? 1 : -1); }'));
}
_osort($input, 'id');
foreach($result as $row):?>
<div class="trigger">
<p class="resume_date"><?=$row->date?></p>
<h2 class="resume_company"><a href="#"><?=$row->company?></a> </h2>
</div>
<div class="toggle_container">
<div class="block">
<h1 class="resume-title"><?=$row->job_title?></h1>
<!--Content-->
<p class="resume-location"><?=$row->location?></p>
<div class="clear"></div>
<p class="resume-content"><?=$row->description?></p>
<br />
</div>
</div>
<div class="clear"></div>
<?php endforeach;?>
Perhaps I need to add or put the function call else where?