I would like to sort the data I receive from an API call on multiple criteria and afterwards filter it to display the data in 2 different tables.
This is the code that I have now to sort the data, on 1) machine->id, 2) date, 3) ordering.
function cmp($a, $b)
{
if (strtotime($a->date) == strtotime($b->date)){
return $a->ordering - $b->ordering;
}
if ($a->machine->id == $b->machine->id) {
return strtotime($a->date) - strtotime($b->date);
}
return strcmp($a->machine->id, $b->machine->id);
}
usort($obj, "cmp");
After that I filter only the data with a specific machine->id to show that data in a table:
$machine1 = array_filter($obj, function($object){
return ($object->machine->id == 1141);
});
and
$machine2 = array_filter($obj, function($object){
return ($object->machine->id == 1259);
});
now the data in the table for machine1 looks like this, the date sorting is not working properly:
2018-11-26T23:00:00Z - ordering: 1
2018-11-26T23:00:00Z - ordering: 3
2018-11-27T23:00:00Z - ordering: 2
2018-11-27T23:00:00Z - ordering: 3
2018-11-27T23:00:00Z - ordering: 4
2018-11-27T23:00:00Z - ordering: 5
2018-11-25T23:00:00Z - ordering: 1
2018-11-25T23:00:00Z - ordering: 2
2018-11-26T23:00:00Z - ordering: 2
2018-11-27T23:00:00Z - ordering: 1
2018-11-25T23:00:00Z - ordering: 3
2018-11-25T23:00:00Z - ordering: 4
2018-11-25T23:00:00Z - ordering: 5
2018-11-25T23:00:00Z - ordering: 6
2018-11-25T23:00:00Z - ordering: 7
2018-11-25T23:00:00Z - ordering: 8
2018-11-25T23:00:00Z - ordering: 9
2018-11-25T23:00:00Z - ordering: 10
2018-11-25T23:00:00Z - ordering: 11
2018-11-26T23:00:00Z - ordering: 4
2018-11-26T23:00:00Z - ordering: 5
2018-11-26T23:00:00Z - ordering: 6
2018-11-26T23:00:00Z - ordering: 7
2018-11-26T23:00:00Z - ordering: 8
2018-11-26T23:00:00Z - ordering: 9
2018-11-26T23:00:00Z - ordering: 10
2018-11-26T23:00:00Z - ordering: 11
2018-11-26T23:00:00Z - ordering: 12
2018-11-26T23:00:00Z - ordering: 13
2018-11-26T23:00:00Z - ordering: 14
2018-11-26T23:00:00Z - ordering: 15
2018-11-26T23:00:00Z - ordering: 16
What am I doing wrong?