Search code examples
phplaravellaravel-5.3

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.


Solution

  • 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)