#laravel #sorting #collections
Вопрос:
Я пытаюсь найти лучший способ сортировки по нескольким значениям и нескольким ключам одновременно.
У меня есть следующая «последовательность сортировки»;
- сортировка по «закончено или нет»
- сортировать по «не вовремя или нет»
- сортировка по «набранным очкам»
- сортировка по «баллам за ваше время»
- сортировка по «баллам для каждого»секретного контроля времени» может быть нулевой, тогда вы окажетесь в конце этой сортировки».
- сортировка по «пройденному расстоянию»
В настоящее время у меня есть что-то вроде этого ответа (отсортировано);
[ { "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-gt;sortBy('total') -gt;sortBy('points_for_time') -gt;sortBy('points_for_gtc') // this is an issue, since it puts you below someone else when you have less points -gt;sortBy('points_for_distance') -gt;sortBy('out_of_time') -gt;sortBy('dnf');
У меня есть еще одно «поле» detail_points_for_gtc
, в котором содержатся точки (по порядку) для каждого «контроля времени».
Существует вероятность того, что там ничего не заполнено, если это так, то вы должны быть в конце списка. также возможно, что значение равно нулю. Тогда это должно быть отсортировано в конце.
Я понятия не имею , как я могу сортировать по detail_points_for_gtc
, и если эта сортировка «правильная сортировка»?
Кто-нибудь, кто понимает этот вопрос и, возможно, сможет помочь?
Комментарии:
1. Как вы хотите сравнить два
detail_points_for_gtc
, предполагая, что они не являются нулевыми и не пустыми?2. Его следует «сравнить» следующим образом: зацикливание на всех элементах с соблюдением их порядка. И затем он должен быть отсортирован от самого низкого до самого высокого
Ответ №1:
Я думаю, вы можете попробовать это;
// you can send column and direction from request or where you need $results = $request-gt;direction == 'asc' ? $results-gt;sortBy($request-gt;column) : $results-gt;sortByDesc($request-gt;column);