public function testDateCmpOutOfOrder() { // arrange $a = new DateTime('2014-07-02 09:00:00'); $b = new DateTime('2014-07-01 09:00:00'); // act $result = datecmp($a, $b); // assert $this->assertGreaterThan(0, $result, 'Expected $a to sort after $b'); }
function usort_by(&$array, $sortBy) { if (is_string($sortBy)) { $field = $sortBy; $sortBy = function ($element) use($field) { if (is_object($element)) { return $element->{$field}; } elseif (is_array($element)) { return $element[$field]; } else { return null; } }; } usort($array, function ($a, $b) use($sortBy) { $aValue = $sortBy($a); $bValue = $sortBy($b); if (is_string($aValue) && is_string($bValue)) { return strcmp($aValue, $bValue); } elseif (is_number($aValue) && is_number($bValue)) { return numbercmp($aValue, $bValue); } elseif (is_datetime($aValue) && is_datetime($bValue)) { return datecmp($aValue, $bValue); } }); }
public function sort_manage_accounts($obj1, $obj2) { ///********************************************************//// $sort_by = isset($_GET['sort_by']) ? $_GET['sort_by'] : 'trip_id'; $order = isset($_GET['order']) ? $_GET['order'] : 'desc'; $first_obj = $order == 'asc' ? $obj1 : $obj2; $second_obj = $order == 'asc' ? $obj2 : $obj1; /////**********************************////// $ans = 0; switch ($sort_by) { case "trip_id": if ($first_obj->trip_id > $second_obj->trip_id) { $ans = 1; } else { $ans = -1; } break; case "trip_type": if ($first_obj->type > $second_obj->type) { $ans = 1; } else { if ($first_obj->type < $second_obj->type) { $ans = -1; } else { $ans = 0; } } break; case "tanker": $ans = strcmp($first_obj->tanker->tanker_number, $second_obj->tanker->tanker_number); break; case "entry_date": $ans = datecmp($first_obj->dates->entry_date, $second_obj->dates->entry_date); break; case "product": $ans = strcmp($first_obj->trip_related_details[0]->product->name, $second_obj->trip_related_details[0]->product->name); break; case "route": $route_1 = $first_obj->trip_related_details[0]->source->name . " to " . $first_obj->trip_related_details[0]->destination->name; $route_2 = $second_obj->trip_related_details[0]->source->name . " to " . $second_obj->trip_related_details[0]->destination->name; $ans = strcmp($route_1, $route_2); break; case "stn": $stn_1 = intval($first_obj->trip_related_details[0]->stn_number); $stn_2 = intval($second_obj->trip_related_details[0]->stn_number); //$ans = strcmp($stn_1, $stn_2); if ($stn_1 > $stn_2) { $ans = 1; } else { if ($stn_1 < $stn_2) { $ans = -1; } } break; case "wht": $wht_1 = $first_obj->trip_related_details[0]->get_wht_amount($first_obj->company->wht); $wht_2 = $second_obj->trip_related_details[0]->get_wht_amount($second_obj->company->wht); if ($wht_1 > $wht_2) { $ans = 1; } else { if ($wht_1 < $wht_2) { $ans = -1; } else { $ans = 0; } } break; case "cmp_total_freight": $total_freight_1 = $first_obj->trip_related_details[0]->get_total_freight_for_company(); $total_freight_2 = $second_obj->trip_related_details[0]->get_total_freight_for_company(); if ($total_freight_1 > $total_freight_2) { $ans = 1; } else { if ($total_freight_1 < $total_freight_2) { $ans = -1; } else { $ans = 0; } } break; case "total_freight_for_customer": $total_freight_1 = $first_obj->trip_related_details[0]->get_total_freight_for_customer(); $total_freight_2 = $second_obj->trip_related_details[0]->get_total_freight_for_customer(); if ($total_freight_1 > $total_freight_2) { $ans = 1; } else { if ($total_freight_1 < $total_freight_2) { $ans = -1; } else { $ans = 0; } } break; case "cmp_freight_unit": $cmp_freight_unit_1 = $first_obj->trip_related_details[0]->company_freight_unit; $cmp_freight_unit_2 = $second_obj->trip_related_details[0]->company_freight_unit; if ($cmp_freight_unit_1 > $cmp_freight_unit_2) { $ans = 1; } else { if ($cmp_freight_unit_1 < $cmp_freight_unit_2) { $ans = -1; } else { $ans = 0; } } break; case "customer_freight_unit": $cmp_freight_unit_1 = $first_obj->trip_related_details[0]->customer_freight_unit; $cmp_freight_unit_2 = $second_obj->trip_related_details[0]->customer_freight_unit; if ($cmp_freight_unit_1 > $cmp_freight_unit_2) { $ans = 1; } else { if ($cmp_freight_unit_1 < $cmp_freight_unit_2) { $ans = -1; } else { $ans = 0; } } break; case "company_commission": $company_commission_1 = $first_obj->trip_related_details[0]->get_company_commission_amount($first_obj->company->commission_1); $company_commission_2 = $second_obj->trip_related_details[0]->get_company_commission_amount($second_obj->company->commission_1); if ($company_commission_1 > $company_commission_2) { $ans = 1; } else { if ($company_commission_1 < $company_commission_2) { $ans = -1; } else { $ans = 0; } } break; case "company_paid": $paid_1 = $first_obj->trip_related_details[0]->get_paid_to_company(); $paid_2 = $second_obj->trip_related_details[0]->get_paid_to_company(); if ($paid_1 > $paid_2) { $ans = 1; } else { if ($paid_1 < $paid_2) { $ans = -1; } else { $ans = 0; } } break; case "company_remaining": $remaining_1 = $first_obj->trip_related_details[0]->get_company_commission_amount($first_obj->company->commission_1) - $first_obj->trip_related_details[0]->get_paid_to_company(); $remaining_2 = $second_obj->trip_related_details[0]->get_company_commission_amount($second_obj->company->commission_1) - $second_obj->trip_related_details[0]->get_paid_to_company(); if ($remaining_1 > $remaining_2) { $ans = 1; } else { if ($remaining_1 < $remaining_2) { $ans = -1; } else { $ans = 0; } } break; case "contractor": $contractor_1 = $first_obj->contractor->name; $contractor_2 = $second_obj->contractor->name; $ans = strcmp($contractor_1, $contractor_2); break; case "contractor_freight": $contractor_freight_1 = $first_obj->trip_related_details[0]->get_contractor_freight_amount_according_to_company($first_obj->get_contractor_freight_according_to_company()); $contractor_freight_2 = $second_obj->trip_related_details[0]->get_contractor_freight_amount_according_to_company($second_obj->get_contractor_freight_according_to_company()); if ($contractor_freight_1 > $contractor_freight_2) { $ans = 1; } else { if ($contractor_freight_1 < $contractor_freight_2) { $ans = -1; } else { $ans = 0; } } break; case "contractor_paid": $paid_1 = $first_obj->trip_related_details[0]->get_paid_to_contractor(); $paid_2 = $second_obj->trip_related_details[0]->get_paid_to_contractor(); if ($paid_1 > $paid_2) { $ans = 1; } else { if ($paid_1 < $paid_2) { $ans = -1; } else { $ans = 0; } } break; case "contractor_remaining": $remaining_1 = $first_obj->trip_related_details[0]->get_contractor_freight_amount_according_to_company($first_obj->get_contractor_freight_according_to_company()) - $first_obj->trip_related_details[0]->get_paid_to_contractor(); $remaining_2 = $second_obj->trip_related_details[0]->get_contractor_freight_amount_according_to_company($first_obj->get_contractor_freight_according_to_company()) - $second_obj->trip_related_details[0]->get_paid_to_contractor(); if ($remaining_1 > $remaining_2) { $ans = 1; } else { if ($remaining_1 < $remaining_2) { $ans = -1; } else { $ans = 0; } } break; case "contractor_commission": $contractor_commission_1 = $first_obj->trip_related_details[0]->get_contractor_commission_amount($first_obj->contractor->commission_1); $contractor_commission_2 = $second_obj->trip_related_details[0]->get_contractor_commission_amount($second_obj->contractor->commission_1); if ($contractor_commission_1 > $contractor_commission_2) { $ans = 1; } else { if ($contractor_commission_1 < $contractor_commission_2) { $ans = -1; } else { $ans = 0; } } break; case "customer": $customer_1 = $first_obj->customer->name; $customer_2 = $second_obj->customer->name; $ans = strcmp($customer_1, $customer_2); break; case "customer_net_freight": $customer_commission_1 = $first_obj->trip_related_details[0]->get_customer_freight_amount($first_obj->customer->freight); $customer_commission_2 = $second_obj->trip_related_details[0]->get_customer_freight_amount($second_obj->customer->freight); if ($customer_commission_1 > $customer_commission_2) { $ans = 1; } else { if ($customer_commission_1 < $customer_commission_2) { $ans = -1; } else { $ans = 0; } } break; case "customer_paid": $paid_1 = $first_obj->trip_related_details[0]->get_paid_to_customer(); $paid_2 = $second_obj->trip_related_details[0]->get_paid_to_customer(); if ($paid_1 > $paid_2) { $ans = 1; } else { if ($paid_1 < $paid_2) { $ans = -1; } else { $ans = 0; } } break; case "customer_remaining": $remaining_1 = $first_obj->trip_related_details[0]->get_customer_commission_amount($first_obj->customer->freight) - $first_obj->trip_related_details[0]->get_paid_to_customer(); $remaining_2 = $second_obj->trip_related_details[0]->get_customer_commission_amount($first_obj->customer->freight) - $second_obj->trip_related_details[0]->get_paid_to_customer(); if ($remaining_1 > $remaining_2) { $ans = 1; } else { if ($remaining_1 < $remaining_2) { $ans = -1; } else { $ans = 0; } } break; case "service_charges": $charges_1 = $first_obj->trip_related_details[0]->contractor_benefits(); $charges_2 = $second_obj->trip_related_details[0]->contractor_benefits(); if ($charges_1 > $charges_2) { $ans = 1; } else { if ($charges_1 < $charges_2) { $ans = -1; } else { $ans = 0; } } break; case "billed": $first_num = $first_obj->trip_related_details[0]->bill->id; $second_num = $second_obj->trip_related_details[0]->bill->id; if ($first_num > $second_num) { $ans = 1; } else { if ($first_num < $second_num) { $ans = -1; } else { $ans = 0; } } break; default: if ($first_obj->trip_id > $second_obj->trip_id) { $ans = 1; } else { $ans = -1; } } return $ans; }