laravel - Laravel sortBy 多个值和多个键
问题描述
我试图找出一次按多个值和多个键排序的最佳方法。
我有以下“排序顺序”;
- 按“完成与否”排序
- 按“是否超时”排序
- 按“总分”排序
- 按“你的时间点”排序
- 按“每个‘秘密时间控制’的点数,可以为空,然后你在排序结束时结束”排序
- 按“行驶距离”排序
我目前有这样的响应(排序);
[
{
"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
,如果这种排序是“正确的排序”?
任何理解这个问题并且可以提供帮助的人?
解决方案
我想你可以试试这个;
// you can send column and direction from request or where you need
$results = $request->direction == 'asc' ? $results->sortBy($request->column) : $results->sortByDesc($request->column);
推荐阅读
- product - 从愿望清单中删除禁用的产品
- java - 访问 TextView 中第二行的开头
- cuda - Nvidia Hyper Q 和 Nvidia Streams 有什么区别?
- javascript - 将对象数组中两个属性的唯一值存储到单个数组中
- python - Ruby 的 array.pack() 和 unpack() 的 Python 版本?
- vue.js - 使用 GraphQL 在 Nuxtjs 中检索 Prismic 切片内容在项目中不起作用
- android - 如何在 kotlin(语言环境)中更改语言
- r - 在闪亮的应用程序中调整导航栏菜单的高度
- android - NestedScrollView 内的 RecyclerView 在 NotifyDataSetChanged 后继续滚动 - 问题
- reactjs - 不引用数组时只查看渲染