首页 > 解决方案 > Laravel sortBy 多个值和多个键

问题描述

我试图找出一次按多个值和多个键排序的最佳方法。

我有以下“排序顺序”;

  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,如果这种排序是“正确的排序”?

任何理解这个问题并且可以提供帮助的人?

标签: laravelsortingcollections

解决方案


我想你可以试试这个;

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

推荐阅读