laravel 如何在laravel中按降序对数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49174199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:29:16  来源:igfitidea点击:

How to sort array in descending order in laravel

laravellaravel-5.4

提问by Nida Akram

I have an array which is storing some count and I want to sort that array in descending order. In order to get the element first with greater count. Can anyone please tell me how to sort the array in descending order. I am currently using array_sort() but its sorting in descending order.

我有一个存储一些计数的数组,我想按降序对该数组进行排序。为了首先获得具有更大计数的元素。谁能告诉我如何按降序对数组进行排序。我目前正在使用 array_sort() 但它按降序排序。

回答by Alexey Mezenin

If the structure of the array is simple, use the rsort().

如果数组的结构很简单,请使用rsort().

If not, use the array_sort()and then use the array_reverse():

如果没有,请使用array_sort(),然后使用array_reverse()

$sortedDesc = array_reverse(array_sort(....));

回答by Adnan Mumtaz

in Laravel you can achieve this like

在 Laravel 中,您可以像这样实现

for Ascending

升序

collect($yourArray)->sortBy('Key','ASC')

for Descending

降序

collect($yourArray)->sortBy('Key','DESC');

Hope this helps

希望这可以帮助