dongpan3001 2018-11-27 08:05
浏览 542
已采纳

根据REST API调用中的多个条件进行排序和筛选

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?

  • 写回答

1条回答 默认 最新

  • douzhuo6931 2018-11-27 08:39
    关注

    That happens because you use the machine id in the compare function but after you filter different machines.

    Think of the case:

    1. time = 2015, id = 3, order 2
    2. time = 2016, id = 4, order 3
    3. time = 2013, id = 3, order 3
    

    when comparing 1 and 2 order stays but on 2 and 3 they swap because the ID but the n you will get 1 before 3 which is wrong order for you (dates...)

    I recommend first to split the big array for machine array as you did with:

    $machine1141 = array_filter($obj, function($object){
        return ($object->machine->id == 1141);
    });
    

    Then use compare function as:

    function cmp($a, $b)
    {
        if (strtotime($a->date) == strtotime($b->date))
            return $a->ordering - $b->ordering;
        return strtotime($a->date) - strtotime($b->date);
    }
    usort($machine1141, "cmp");
    

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog