【问题标题】:Laravel sortBy multiple values and multiple keysLaravel sortBy 多个值和多个键
【发布时间】:2021-12-05 17:48:35
【问题描述】:

我正在尝试找出一次按多个值和多个键进行排序的最佳方法。

我有以下“排序顺序”;

  1. 按“完成与否​​”排序
  2. 按“是否超时”排序
  3. 按“总分”排序
  4. 按“时间积分”排序
  5. 按“每个'秘密时间控制'的点数排序,可以为空,然后你会在排序结束时结束”
  6. 按“行驶距离”排序

我目前有类似这样的响应(已排序);

[
    {
        "team_number": 201,
        "points_for_time": 0,
        "detail_points_for_time": {
            "tc_start": 0,
            "tc_round_in": 0,
            "tc_stop": 0
        },
        "points_for_gtc": 1,
        "detail_points_for_gtc": [
            0,
            1
        ],
        "points_for_distance": 0,
        "missed_controls": 100,
        "out_of_time": false,
        "dnf": false,
        "total": 101
    },
    {
        "team_number": 202,
        "points_for_time": 2,
        "detail_points_for_time": {
            "tc_start": 0,
            "tc_round_in": 0,
            "tc_stop": 2
        },
        "points_for_gtc": 0,
        "detail_points_for_gtc": [],
        "points_for_distance": 0,
        "missed_controls": 100,
        "out_of_time": false,
        "dnf": false,
        "total": 102
    },
    {
        "team_number": 203,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 204,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 205,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 206,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 207,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 208,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    },
    {
        "team_number": 209,
        "points_for_time": 0,
        "detail_points_for_time": 0,
        "points_for_gtc": 0,
        "detail_points_for_gtc": 0,
        "points_for_distance": 0,
        "missed_controls": 0,
        "out_of_time": false,
        "dnf": true,
        "total": 0
    }
]

所以这里的目标是,我根据上面的“序列”得到一个完整的排序。

我现在用这个;

$results = $results->sortBy('total')
    ->sortBy('points_for_time')
    ->sortBy('points_for_gtc') // this is an issue, since it puts you below someone else when you have less points
    ->sortBy('points_for_distance')
    ->sortBy('out_of_time')
    ->sortBy('dnf');

我还有另一个“字段”detail_points_for_gtc,它保存每个“时间控制”的点(按顺序)。

存在没有填写任何内容的可能性,如果是这样,那么您应该在列表的末尾。该值也可能为空。那么它应该在最后进行排序。

我不知道如何对detail_points_for_gtc 进行排序,如果这种排序是“正确的排序”?

有谁理解这个问题并且可以提供帮助?

【问题讨论】:

  • 假设两个detail_points_for_gtc不为空也不为空,你想如何比较它们?
  • 它应该被“比较”如下:循环遍历所有项目并尊重它们的顺序。然后,它应该从低到高排序

标签: laravel sorting collections


【解决方案1】:

我想你可以试试这个;

// you can send column and direction from request or where you need
$results = $request->direction == 'asc' ? $results->sortBy($request->column) : $results->sortByDesc($request->column);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2015-03-04
    • 2012-07-12
    相关资源
    最近更新 更多