Laravel 的 array_sort 助手 DESC e ASC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24612944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel's array_sort helper DESC e ASC
提问by almo
I want to sort a multidimensionl array by one or more keys using the Laravel helper array_sort
.
我想使用 Laravel 助手按一个或多个键对多维数组进行排序array_sort
。
array(
array('firstname_1','lastname_1'),
array('firstname_2','lastnmae_2')
)
I want to order it first by firstname and then by lastname.
我想先按名字排序,然后按姓氏排序。
I also want to do this in DESC or ASC order. How can I achieve this?
我也想按 DESC 或 ASC 顺序执行此操作。我怎样才能做到这一点?
There are functions aivalable in the internet to do this but I would like to understand how to use the Laravel helper. The doc for array_sort (http://laravel.com/docs/helpers#arrays) I don't find comprehensive.
互联网上有一些功能可以做到这一点,但我想了解如何使用 Laravel 助手。array_sort ( http://laravel.com/docs/helpers#arrays)的文档我没有找到全面的。
回答by Aken Roberts
The array_sort()
helper function is a very thin wrapper around the default Illuminate\Support\Collection::sortBy()
method. Excluding comments, this is all that it does:
该array_sort()
辅助函数是默认的非常薄的包装Illuminate\Support\Collection::sortBy()
方法。不包括评论,这就是它所做的一切:
function array_sort($array, Closure $callback)
{
return \Illuminate\Support\Collection::make($array)->sortBy($callback)->all();
}
While handy, it is limiting in its sorting capabilities. Similarly, the Collection class will allow you to change sort direction, but not much else.
虽然方便,但它的分类能力有限。同样,Collection 类将允许您更改排序方向,但除此之外别无他法。
In my opinion, you have two options:
在我看来,您有两种选择:
Skip a Laravel-only solution and use some normal PHP and
array_multisort()
. As @Joncommented, there's some great details in this SO question.Use a combination of grouping and sorting in a Collection object to achieve the results you want.
跳过 Laravel-only 解决方案并使用一些普通的 PHP 和
array_multisort()
. 正如@Jon评论的那样,这个 SO question 中有一些很好的细节。在 Collection 对象中使用分组和排序的组合来实现您想要的结果。
I'd just stick with #1.
我只是坚持#1。
回答by maxwilms
Just as an example how to sort by first name and then by last name with the sort helper of Laravel.
举个例子,如何使用 Laravel 的排序助手按名字排序,然后按姓氏排序。
First define some more example data:
首先定义一些更多的示例数据:
$array = array(
array('firstname_1','lastname_1'),
array('firstname_2','lastname_3'),
array('firstname_2','lastname_2'),
array('firstname_3','lastname_3'),
);
In our closure we want to sort by first name first, and then by last name. Laravel will sort by the returned values of the closure. So in your case the trick is to concatenateboth strings:
在我们的闭包中,我们想先按名字排序,然后按姓氏排序。Laravel 将根据闭包的返回值进行排序。所以在你的情况下,诀窍是连接两个字符串:
$array = array_sort($array, function($value) {
return sprintf('%s,%s', $value[0], $value[1]);
});
Internally Laravel will now sort the contents of this intermediate array:
Laravel 现在将在内部对这个中间数组的内容进行排序:
$intermediateArray = array(
'firstname_1,lastname_1',
'firstname_2,lastname_3',
'firstname_2,lastname_2',
'firstname_3,lastname_3',
);
This will result in an array which is sorted by first name and than by last name in ascendingorder.
这将产生一个按名字排序的数组,而不是按姓氏升序排序。
To use descendingorder you need first sort the array and than reverse it:
要使用降序,您首先需要对数组进行排序,然后将其反转:
$array = array_reverse(array_sort($array, function($value) {
return sprintf('%s,%s', $value[0], $value[1]);
}));