方式1 根据集合拼接key去排序
$results_product = [['create_time' => '2018-10-10 10:25:05', 'downloaded' => 20, 'product_id' => 1],['create_time' => '2018-10-10 10:20:05', 'downloaded' => 50, 'product_id' => 2],['create_time' => '2018-10-10 10:20:05', 'downloaded' => 50, 'product_id' => 4],['create_time' => '2018-10-10 10:20:05', 'downloaded' => 70, 'product_id' => 3]];$result = collect($results_product)->map(function($data){$data['sort'] = $data['create_time'].'_'.$data['downloaded'].'_'.$data['product_id'];return $data;})->sortByDesc('sort')->toArray();dd($result);
不方便的就是只能单向排序 如果是 字段1 升序 字段2 降序 就不太好处理了
方式2 PHP函数处理(推荐,且灵活)
foreach ($results_product as $key => $row) {$create_time[$key] = $row['create_time'];$downloaded[$key] = $row['downloaded'];$product_id[$key] = $row['product_id'];}array_multisort($create_time, SORT_DESC, $downloaded, SORT_DESC, $product_id, SORT_DESC, $results_product);
方式3 (误区-该方法不行,多字段切勿使用)
$results_product2 = collect($results_product);$results_product2 = $results_product2->sortByDesc('create_time')->sortByDesc('downloaded')->sortByDesc('product_id');