dpgu5074 2016-03-29 14:41
浏览 8
已采纳

怪异的Usort行为

I've created an array with multiple objects, these objects have a startTime and a endTime. first i need the lowest startTime and then the higest endTime

array{
   [1541] =>  object(task)#430 (2){ ["startTime"]=> string(19) "2016-03-24 06:29:35" ["endTime"]=> string(19) "2016-03-24 06:31:35"}
   [1545] =>  object(task)#431 (2){ ["startTime"]=> string(19) "2016-03-24 07:20:50" ["endTime"]=> string(19) "2016-03-24 07:25:50"}
}

So for this example the lowest startTime would be "2016-03-24 07:25:50" and the highest endTime would be "2016-03-24 07:25:50". But the weird thing is that this isn't (always) the result i'm getting (3 out of 5 times)

the result is lowest startTime is "2016-03-24 07:25:50" and the highest endTime is "2016-03-24 06:29:35"

I order these objects using usort based on the startTime attribute. Here is te code i'm using

class StaticsComponent extends CComponent  {

    public static  function cmp_start($a, $b) {
        if (microtime($a->startTime) == microtime($b->startTime)) {
           return 0;
        }
        return (microtime($a->startTime) > microtime($b->startTime)) ? -1 : 1;
    }
    public static  function cmp_end($a, $b) {
        if (microtime($a->startTime) == microtime($b->startTime)) {
           return 0;
        }
        return (microtime($a->startTime) < microtime($b->startTime)) ? -1 : 1;
    }

}

class OrderBcStats extends Orderbc {
    public $tasks;

    public $startTime;
    public $endTime;

    public function process(){
        if(empty($this->tasks)){
           return;
        }
        $starts = $this->tasks;
        $ends = $this->tasks;

        usort($starts, array('StaticsComponent','cmp_start'));
        usort($ends, array('StaticsComponent','cmp_end'));

        $first = reset($starts);
        $last = end($ends);

        $this->startTime = $first->startTime;
        $this->endTime = $last->endTime;
    }
}

additional info: this is a Yii project, PHP Version 5.4.40, OS Centos, apache 2.0

  • 写回答

1条回答 默认 最新

  • drn61317 2016-03-29 16:05
    关注

    As per the docs "microtime() returns the current Unix timestamp with microseconds" (emphasis added). The only param it takes is a boolean $get_as_float. So you are not sorting according to the times of the items, but according (roughly) to their order.

    You could create DateTime objects and compare their timestamps. however, if you are certain of the format then you could exploit the fact that for ISO format dates, alphabetical order and date order are the same thing...

    So I guess instead of

            if (microtime($a->startTime) == microtime($b->startTime)) {
               return 0;
            }
            return (microtime($a->startTime) > microtime($b->startTime)) ? -1 : 1;
    

    you want something like

    if ($a->startTime == $b->startTime) {
       return 0;
    }
    return ($a->startTime > $b->startTime) ? -1 : 1;
    

    or alternatively

    $a_time = new DateTime($a->startTime);
    $b_time = new DateTime($a);
    if ($a_time == $b_time) {
       return 0;
    }
    return ($a_time > $b_time) ? -1 : 1;   
    

    Warning: not tested, but should work.

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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等