dqv2743 2018-07-12 11:28
浏览 29
已采纳

Laravel中的递归排序

I have a problem creating a sorted collection recursively... Suppose I have a collection of items and each item has a description of the one that goes right in front of it. Many items can have the same item in front. If the item is in front of the line, the value is 0.

The problem with the following method which belongs inside a Class, is that it is returning only the first line items of the collection, the ones that are supposed to be in front.

    /**
     * Returns a collection of items
     *
     * @param \Illuminate\Support\Collection $list
     * @param int $prev
     *
     * @return \Illuminate\Support\Collection
     */
    public function getSortOrder( $list, $prev = 0 ){
        $result = collect();
        $with_prevs = $list->filter( function( $item ) use ( $prev ){
            return $item->data[ 'prev' ] == $prev;
        } );
        $list = $list->diff( $with_prevs );
        if( $with_prevs ->count() > 0 ){
            foreach( $with_prevs as $with_prev ){
                $result->push( $with_prev );
                if( $list->count() > 0 ){
                    $result->concat( $this->getSortOrder( $list, $with_prev->id ) );
                }
            }
        }
        return $result;
    }
  • 写回答

1条回答 默认 最新

  • douwen1006 2018-07-12 18:36
    关注

    For some reason, using concat or merge when the recursion is called, will not work. So the solution is simple, assign the resulting collection from the recursion to a variable and cycle through it using the push method and this will solve the problem.

        /**
         * Devuelve una colección de preguntas en orden
         *
         * @param \Illuminate\Support\Collection $list
         * @param int $prev
         *
         * @return \Illuminate\Support\Collection
         */
        public function getOrdenPreguntas( $listado, $prev = 0 ){
            $result = collect();
            $preguntas = $listado->filter( function( $item ) use ( $prev ){
                return $item->datos[ 'prev' ] == $prev;
            } );
            $listado = $listado->diff( $preguntas );
            if( $preguntas->count() > 0 ){
                foreach( $preguntas as $pregunta ){
                    $result->push( $pregunta );
                    if( $listado->count() > 0 ){
                        $extras = $this->getOrdenPreguntas( $listado, $pregunta->id );
                        foreach( $extras as $extra ){
                            $result->push( $extra );
                        }
                    }
                }
            }
            return $result;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数