Laravel按日期对集合进行排序

Laravel按日期对集合进行排序

本文介绍了Laravel按日期对集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个收集结果:

$result = [{
    "date": "2016-03-21",
    "total_earned": "101214.00"
},
{
    "date": "2016-03-22",
    "total_earned": "94334.00"
},
{
    "date": "2016-03-23",
    "total_earned": "96422.00"
},
{
    "date": "2016-02-23",
    "total_earned": 0
},
{
    "date": "2016-02-24",
    "total_earned": 0
},
{
    "date": "2016-02-25",
    "total_earned": 0
}]

我想按日期对结果进行排序:

I want to sort the result by date:

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

但是我没有得到预期的结果:

But I don't get the expected result:

[{
    "date": "2016-02-23",
    "total_earned": 0

},
{
    "date": "2016-02-24",
    "total_earned": 0
},
{
    "date": "2016-02-25",
    "total_earned": 0
},
{
    "date": "2016-03-22",
    "total_earned": "94334.00"
},
{
    "date": "2016-03-21",
    "total_earned": "101214.00"
},
{
    "date": "2016-03-23",
    "total_earned": "96422.00"
}]

如您所见,第2个月的所有内容均已正确排序.但是在第3个月开始混乱了. (实际结果比这更长,并且从第3个月开始混乱)

As you can see all with month 2 is sort properly. However at month 3 it start messed up. (the real result is longer than this and it messed up start at month 3)

有什么方法可以使其正确排序吗?

Any solution to make it sort properly?

谢谢.

推荐答案

尝试一些尝试像这样:

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

这篇关于Laravel按日期对集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:02