简体   繁体   English

Laravel集合没有正确排序

[英]Laravel collection not properly sort

I try to sort my collection using Laravel function. 我尝试使用Laravel函数对我的收藏进行排序。

here is my data for example: 这是我的数据,例如:

$collection = 
 [
  {
    "total_earned": "31739",
    "total_spent": "0",
    "total_amount": "317390",
    "date": "2015-10-01"
  }, {
    "total_earned": "212622",
    "total_spent": "86943",
    "total_amount": "2213165",
    "date": "2015-12-01"
  }, {
    "total_earned": 0,
    "total_spent": 0,
    "total_amount": 0,
    "date": "2015-10-29"
  }, {
    "total_earned": 0,
    "total_spent": 0,
    "total_amount": 0,
    "date": "2015-10-30"
  }
];

When I try to sort using Laravel sortBy function and return it become like this: 当我尝试使用Laravel sortBy函数进行排序并返回时,它变成这样:

[{
    "total_earned": 0,
    "total_spent": 0,
    "total_amount": 0,
    "date": "2015-10-29"
}, {
    "total_earned": 0,
    "total_spent": 0,
    "total_amount": 0,
    "date": "2015-10-30"
}, {
    "total_earned": "212622",
    "total_spent": "86943",
    "total_amount": "2213165",
    "date": "2015-12-01"
}, {
    "total_earned": "31739",
    "total_spent": "0",
    "total_amount": "317390",
    "date": "2015-10-01"
}]

As you can see date 2015-10-01 is at bottom. 如您所见,日期2015-10-01在底部。 It supposed to be before 2015-10-29 . 应该在2015-10-29之前。

My code to sort: 我的代码进行排序:

return $transaction->sortBy('date')->values()->all();

Is this a bug or the sort suppose to be like that? 这是一个错误还是某种假设?


Update my entire code: 更新我的整个代码:

 $transaction = Transaction::dateRange($startDate->format('Ym-d'), $endDate->format('Ym-d')) ->groupBy('merchant_branch_id', DB::raw('DATE(created_at)')) ->selectRaw('DATE(created_at) as date, SUM(point_earned) as total_earned, SUM(point_spent) as total_spent, FLOOR(SUM(amount)) as total_amount') ->orderBy('created_at') ->get(); $convert = $transaction->map(function($item, $key) { $date = Carbon::parse($item->date)->format('Ym-d'); return [$date]; }); $convert = $convert->toArray(); $period = new DatePeriod($startDate, new DateInterval('P1D'), $endDate); foreach ($period as $row) { $date = $row->format('Ym-d'); if (!in_array_r($date, $convert)) { $transaction->push(['date' => $date, 'total_earned' => 0, 'total_spent' => 0, 'total_amount' => 0]); } } $sorted = $transaction->sortBy('date')->values()->all(); return $sorted; 

Just in case anyone else arrives at this question with the same problem, you can try with the following code from this answer https://stackoverflow.com/a/36170075/1208242 : 万一其他人遇到同样的问题,您可以尝试从此答案https://stackoverflow.com/a/36170075/1208242使用以下代码:

$sorted = $transaction->sortBy(function($col)
{
    return $col;
})->values()->all();

It might give you the dates in the wrong order (sortBy sorts in 'ascending' order by default, which apparently returns descending order when working with dates on Ymd format), so you could use sortByDesc() method instead 它可能会给您日期错误的顺序(默认情况下,sortBy以“升序”排序,当使用Ymd格式的日期时,显然会返回降序),因此您可以改用sortByDesc()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM