【问题标题】:Sorting Minutes排序分钟
【发布时间】:2017-12-14 00:46:09
【问题描述】:

我正在尝试对足球分钟数进行排序,但排序不正确,例如,在足球比赛中,分钟数非常正常,例如“1,2,3,40+5...”。基本上是说40分钟+ 5分钟(45分钟)。 所以在我的记录中,我有一个类似的集合:

[
  {
    "id": 9876,
    "minute": "90+30",
  },
  {
    "id": 9874,
    "minute": "90+10",
  },
  {
    "id": 9873,
    "minute": "105",
  },
  {
    "id": 9873,
    "minute": "90",
  },
 ...
]

所以要让它正确排序分钟,我需要在字符串“分钟”上使用爆炸,而不是创建一个 array_sum,但在我的代码中,它仍然没有正确排序,仍然在 105 高于“90+” 10 英寸。

这是我的代码:

$timelines = SoccerTime::where('match_id',$id)
        ->orderBy('minute', 'desc')
        ->get();
$collection = collect($timelines);
$sorted = $collection->sortByDesc('minute');
$test = $sorted->values()->all();

//Here i begin the new Sort
$newSort =  collect($test)->sort(function ($a, $b) {
    return array_sum(explode("+",$a->minute)) -  array_sum(explode("+",$b->minute));
});
return  $newSort;

【问题讨论】:

  • 为什么使用“90+10”而不是“100”?即使您想使用“90+10”,您也可以同时存储一个用于显示和一个用于排序
  • 你给我一个想法,我创建一个属性,我总结它,然后按这个排序,谢谢

标签: php laravel sorting laravel-5 collections


【解决方案1】:

我认为您应该更改模型中的几个字段。我会使用minute 字段来存储实际分钟(100 在您的“90+10”示例中)和另一个整数字段period 来指示“句点”,其中:

- 1 1T
- 2 2T
- 3 1ET
- 4 2ET

所以你的新排序方式应该是: 首先是period(解决您在 1ET 的 93' 之前获得 2T 的 90'+4' 的问题),然后是 minute(实际排序)。

这会容易得多,它还可以帮助您进行其他一些报告查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2013-01-25
    • 1970-01-01
    • 2021-05-14
    • 2021-11-03
    相关资源
    最近更新 更多