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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀