简体   繁体   English

在Laravel 5.3集合中使用version_compare

[英]Using version_compare in Laravel 5.3 collection

So I have a collection that has a property with a version number as a string. 因此,我有一个集合,该集合的属性带有版本号作为字符串。 I want to sort the collection on the version number. 我想按版本号对集合进行排序。

I found this code online, but it takes a one dimensional array as the first argument. 我在网上找到了此代码,但是它使用一个一维数组作为第一个参数。

usort($a, 'version_compare');

Any ideas on how accomplish this? 关于如何做到这一点的任何想法?

Maybe I could use pluck and sort with the above code, then use toArray() on the original collection and sort by the sorted array. 也许我可以使用pluck并对上面的代码进行排序,然后在原始集合上使用toArray()并按已排序的数组进行排序。 I believe there is a function for that somewhere. 我相信某个地方有该功能。 But if any of you have another idea I will be happy to hear it. 但是,如果您有其他想法,我会很高兴听到。

The answer is to set the sort method as the second argument. 答案是将sort方法设置为第二个参数。 Like this: 像这样:

$plugin->versions->sortBy('version', SORT_NATURAL)

This will do the trick :o) 这将达到目的:o)

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

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