function CourseList::sort_most_recent_first
Search API
7.x CourseList.php | CourseList::sort_most_recent_first($bool_maintain_alpha_order = true) |
6.x CourseList.php | CourseList::sort_most_recent_first($bool_maintain_alpha_order = true) |
This method will sort by the most recently taken courses (determined by the term_id). The easiest way I can think to do this is to temporarily put their term_id's and index#'s into an array, and then have PHP sort the array itself. PHP's sorting algorithm is faster than anything I can program right now, anyway.
Parameters
bool $bool_maintain_alpha_order:
File
- classes/
CourseList.php, line 1220
Class
Code
function sort_most_recent_first($bool_maintain_alpha_order = true)
{
$tarray = array();
// Since I need the indexes, I will have to go through the array
// myself...
for ($t = 0; $t < $this->count; $t++)
{
$c = $this->array_list [$t];
$cn = "";
if ($bool_maintain_alpha_order == true)
{
// We say 1000- the course number in order to give
// us the complement of the number. That is so it will
// reverse-sort in the correct order. Strange, but it fixes
// a small display issue where PHYS 207 and PHYS 209, taken at
// the same time, causes PHYS 209 to be displayed first.
// We also reverse the subject_id, again, so that
// MATH will be sorted above ZOOL, when taken at the same time.
// This might not work at all, though...
// TODO: The 1000 - course_num bit doesn't work (throws a warning) if the course_num
// is non-numeric. Ex: 301A. Let's instead use the same strrev() function on course_num, but we put it in quotes
// to guarantee it gets evaluated as a string.
//$cn = strrev($c->subject_id) . "," . (1000 - $c->course_num);
$cn = strrev($c->subject_id) . "," . strrev("$c->course_num");
}
$str = "$c->term_id ~~ $cn ~~ $t";
array_push($tarray, $str);
}
// Now, sort the array...
rsort($tarray);
// Now, convert the array back into a list of courses.
$new_list = new CourseList();
for ($t = 0; $t < count($tarray); $t++)
{
$temp = explode(" ~~ ", $tarray [$t]);
$i = $temp [2];
$new_list->add($this->array_list [$i]);
}
// Okay, now $new_list should contain the correct values.
// We will transfer over the reference.
$this->array_list = $new_list->array_list;
}