简体   繁体   中英

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. 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. Like this:

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

This will do the trick :o)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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