dongzhouhao4316 2012-04-18 08:19
浏览 57

为什么我的排序算法不起作用?

I'm developing a system for a client that creates a csv of packing labels which is sent to a printer. The client has six different items. Customers order products in bulk from my client. Two items (product A and product B) share the same packing line. In order to make packing more efficient my client wants to alternate between packing product A and packing product B first.

For example, if John, Sally, and James all ordered both products, the system needs to write John's orders to the csv starting with product A, Sally's orders starting with product B, and James' orders starting with product A again.

I've pasted my non-working code below, but this is really screwing with my head and I'm having a really tough time with it.

foreach($orders as $order) {

    $name = null;
    $phone = null;

    $account = DAO_ContactPerson::get($order->account_id);
    $delivery = false;
    if($account->is_agency) {
        $name = $order->getAttribute('name');
        $phone = $order->getAttribute('phone');
    } else {
        $name = sprintf("%s %s",
            $account->getPrimaryAddress()->first_name,
            $account->getPrimaryAddress()->last_name
        );
        $phone = $account->phone;
    }

    $name = trim($name);
    $phone = trim($phone);
    $items = $order->getItems();

    if($order->getAttribute('delivery')) {
        $type = 'deliveries';
        $destination = 'Delivery';
        $address = sprintf("%s %s %s",
            $order->getAttribute('delivery_address.line1'),
            $order->getAttribute('delivery_address.line2'),
            $order->getAttribute('delivery_address.postal')
        );
    } else {
        $type = 'pickups';
        $agency = DAO_ContactPerson::getAgency($order->getAttribute('pickup'));
        $destination = $agency->name;

        // Override account id so orders are grouped by agency
        $order->account_id = $agency->id;
        $address = null;
    }
//          var_dump($order->id);
    // Init account array
    if(!isset($rows[$type][$order->account_id]))
        $rows[$type][$order->account_id] = array('combined' => array(), 'separate' => array());

    foreach($items as $item) {
        $packing = 'separated';
        if($item->product_id == 3 || $item->product_id == 4)
            $packing = 'combined';

        if(!isset($rows[$type][$order->account_id][$packing][$item->product_id]))
            $rows[$type][$order->account_id][$packing][$item->product_id] = array();
        $i = 0;
        while($i < $item->quantity) {
            $rows[$type][$order->account_id][$packing][$item->product_id][] = array(
                    'number' => $order->id,
                    'destination' => $destination,
                    'size' => $item->product_id,
                    'name' => $name,
                    'address' => $address,
                    'phone' => $phone
                );
            $i++;
        }

    }
//          if($order->id == 176) {
//              var_dump($rows[$type][$order->account_id][$packing]);
//          }
}

$this->weight = 1;

$pickups = count($rows['pickups']);
for($i = 0; $i < $pickups; $i++) {
    $account =& $rows['pickups'][$i];
    $account['output'] = array();
    if(isset($account['combined'])) {
        $combined_products =& $account['combined'];
        if(!empty($combined_products)) {
            foreach($combined_products as $prod_id => $combined) {
                usort($combined_products[$prod_id], array($this, "_compareBoxes"));
            }
            // Flip weights once we finish with this account
            $last_box = end($combined_products);
            $last_box = array_pop($last_box);
            reset($combined_products);
            if($this->weight == 1) {
                $this->weight = -1;
                if($last_box['size'] == 3) {
                    asort($combined_products);
                }
            } else {
                if($last_box['size'] == 4) {
                    arsort($combined_products);
                }
                $this->weight = 1;
            }
            foreach($combined_products as $combined) {
                $account['output'][] = $combined;
            }
            foreach($account['separated'] as $separate) {
                $account['output'][] = $separate;
            }
        }
    } else {
        if(isset($account['separated']))
            $account['output'] = $account['separated'];
    }
}

$deliveries = count($rows['deliveries']);
for($i = 0; $i < $deliveries; $i++) {
    $account =& $rows['deliveries'][$i];
    $account['output'] = array();
    if(isset($account['combined'])) {
        $combined_products =& $account['combined'];
        if(!empty($combined_products)) {
            foreach($combined_products as $prod_id => $combined) {
                usort($combined_products[$prod_id], array($this, "_compareBoxes"));
            }
            // Flip weights once we finish with this account
            $last_box = end($combined_products);
            $last_box = array_pop($last_box);
            reset($combined_products);
            if($this->weight == 1) {
                $this->weight = -1;
                if($last_box['size'] == 3) {
                    asort($combined_products);
                }
            } else {
                if($last_box['size'] == 4) {
                    arsort($combined_products);
                }
                $this->weight = 1;
            }
            foreach($combined_products as $combined) {
                $account['output'][] = $combined;
            }
            foreach($account['separated'] as $separate) {
                $account['output'][] = $separate;
            }
        }
    } else {
        if(isset($account['separated']))
            $account['output'] = $account['separated'];
    }
}

$rows['output'] = $rows['pickups'];
array_push($rows['output'], $rows['deliveries']);

$output = '';
foreach($rows['output'] as $account_id => $boxes) {
    if(!empty($boxes['output'])) {
        foreach($boxes['output'] as $labels) {
            if(!empty($labels)) {
                foreach($labels as $label) {
                    $output .= implode(',', $label) . "<br>";
                }
            }
        }
    }
}

The _compareBoxes method looks like this:

private function _compareBoxes($a, $b) {
    if($a['size'] == $b['size']) {
        return 0;
    }
    if($this->weight == 1) {
        // Medium first, then Large
        return ($a['size'] < $b['size']) ? -1 : 1;
    }
    if($this->weight == -1) {
        // Large first, then Medium
        return ($a['size'] > $b['size']) ? -1 : 1;
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题