I have an array of objects (goals) that I want to sort based on 2 of their properties, the Status
and the DueDate
.
Here are the rules:
Statuses:
- Design
- Approved
- In Progress
- Completed
- Archived
If a goal has a status of 4 (completed) or 5 (archived) then the DueDate
doesn't matter.
If a goal is neither 4 nor 5 and its DueDate
is less than now then it is "Overdue" and should be at the top
If a goal is not "Overdue" then the order of statuses determines it's position (lowest to highest)
If $a
and $b
are both "Overdue" then the one with the earliest DueDate
is more important
The order should be:
- Overdue
- Design
- Approved
- In Progress
- Completed
- Archived
Here is the last thing I tried:
function cmp($a, $b)
{
$now = new DateTime("now");
$aDueDate = new DateTime($a->GetDueDate());
$bDueDate = new DateTime($b->GetDueDate());
if($a->GetStatus() != 4 && $a->GetStatus() != 5 && $b->GetStatus() != 4 && $b->GetStatus() != 5){
if($aDueDate < $now || $bDueDate < $now){
if($aDueDate == $bDueDate){
return 0;
}
return ($aDueDate < $bDueDate) ? -1 : 1;
}
}
elseif(($a->GetStatus() == 4 || $a->GetStatus() == 5) && ($b->GetStatus() != 4 && $b->GetStatus() != 5)) {
return -1;
}
elseif(($a->GetStatus() != 4 && $a->GetStatus() != 5) && ($b->GetStatus() == 4 || $b->GetStatus() == 5)){
return 1;
}
if ($a->GetStatus() == $b->GetStatus()) {
return 0;
}
return ($a->GetStatus() < $b->GetStatus()) ? -1 : 1;
}
Which orders the array like so:
- Completed
- Archived
- Overdue
- Design
- Approved
- In Progress