ArrayList.Sort是啥效果?从大到小 还是 从小到大

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArrayList.Sort是啥效果?从大到小 还是 从小到大相关的知识,希望对你有一定的参考价值。

是将ArrayList从小到大进行排序。 参考技术A 从小到大

Laravel eloquent 从不一定有关系的表关系中将数据从大到小排序

【中文标题】Laravel eloquent 从不一定有关系的表关系中将数据从大到小排序【英文标题】:Laravel eloquent sorts data from largest to smallest from table relations that do not necessarily have a relationship 【发布时间】:2021-12-04 13:33:36 【问题描述】:

当从不一定与 PHP Laravel Eloquent 有关系的表关系中对数据进行从大到小排序时,我遇到了问题。

Table: items
|---------------------------|
|  id  |  name    |  price  |
|---------------------------|
|  1   |  Samsung |  70.000 |
|  2   |  iPhone  |  90.000 |
|  3   |  Nokia   |  50.000 |
|  4   |  Huawei  |  80.000 |
|  5   |  Xiaomi  |  60.000 |
|  6   |  LG      |  40.000 |
|---------------------------|

Table: sells
|------------------------------------------|
|  id  |  invoice |  total   |  created_at |
|------------------------------------------|
|  1   |   1001   |  720.000 |  2021-10-01 |
|  2   |   1002   |  420.000 |  2021-10-01 |
|  3   |   1003   |   80.000 |  2021-10-15 |
|------------------------------------------|

Table: sell_items
|------------------------------------|
|  id  |  sell_id |  item_id  |  qty |
|------------------------------------|
|  1   |    1     |     1     |   5  |
|  2   |    1     |     2     |   4  |
|  3   |    2     |     3     |   3  |
|  4   |    2     |     2     |   3  |
|  5   |    3     |     4     |   1  |
|------------------------------------|

我只会从最大的数据中提取 5 个数据。

Top Product:
|--------------------------------|
|  No  |  Product |  Total (Qty) |
|--------------------------------|
|  1   |  iPhone  |      7       |
|  2   |  Samsung |      4       |
|  3   |  Nokia   |      3       |
|  4   |  Huawei  |      1       |
|  5   |  Xiaomi  |      0       |
|--------------------------------|

我的语法:

$thisYear = date('Y');
$topProduct = SellItem::whereHas('sells', function($p) use ($thisYear) 
                                        $p->whereYear('created_at', $thisYear)
                                    )
                        ->whereHas('items')
                ->select('id', 'name', DB::raw('sum('qty') as total'))->take(5)->orderBy('total', 'desc')->get();

请帮我解决。

【问题讨论】:

SellItemsell_items 表的数据透视模型还是items 表的模型? @Erin SellItem 是 sell_items 表。 【参考方案1】:

假设:

SellItem 是数据透视表的模型 sell_items Item 是桌子上的模型items Sell 是桌子上的模型sells SellItem 具有 BelongsTo 关系 itemItem SellItem 具有 BelongsTo 关系 sellSell

使用模型类中设置的关系,您可以执行以下操作:

$topProducts = SellItem::has('item')
   ->with('item')
   ->whereHas('sell', function ($query) 
     $query->whereYear('created_at', date('Y'));
   )
   ->groupBy('item_id')
   ->withSum('qty as total')
   ->orderByDesc('total')
   ->get();
   ->map( function ($sell) 
      return $sell->item;
   );

【讨论】:

以上是关于ArrayList.Sort是啥效果?从大到小 还是 从小到大的主要内容,如果未能解决你的问题,请参考以下文章

每个邻接链表中的边结点都是按照序号从大到小的顺序链接而成是啥意思?

-课后作业)

在matlab中从大到小排序

当前目录下,按照文件磁盘空间大小,从大到小排序

用Python写一个 输入10个数,从大到小排序,并输出前五个数据的和 输入 9 8 7 6 5 4 3 2 1 输出35?

用php定义一个数组,要求把数组从大到小排序并输出