一張券先按照金額排序,金額相同的再按照時間排序

<?php

class Discount
{
    public $money;
    public $time;

    function __construct($money, $time)
    {
        $this->money = $money;
        $this->time = $time;
    }
}

$discounts = [];
for ($i = 0; $i < 10; $i++) {
    $discount = new Discount(rand(1, 10), time() - rand(1111, 9999));
    array_push($discounts, $discount);
}
echo '<pre>';
// print_r($discounts);exit;

function quick_sort($ds)
{
	if ( count($ds) <= 1)
	{
		return $ds;
	}
	else
	{
		$left = [];
		$right = [];

		for ( $i = 1; $i < count($ds); $i++)
		{
			if( $ds[$i]->money > $ds[0]->money)
			{
				array_push($left, $ds[$i]);
			}
			else if ( $ds[$i]->money < $ds[0]->money)
			{
				array_push($right,$ds[$i]);
			}
			else
			{
				if ( $ds[$i]->time <= $ds[0]->time)
				{
					array_push($right,$ds[$i]);
				}
				else
				{
					array_push($left,$ds[$i]);
				}
			}
		}

		$left = quick_sort($left);
		$right = quick_sort($right);

		return array_merge($left,[$ds[0]],$right);
	}
}

$result = quick_sort($discounts);
print_r($result);
?>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章