[10.x] Support sort option flags on sortByMany Collections #50269
+103
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
sortBy()
method of Collections allows you to pass sorting option flags as the second parameter, which works when the collection is being sorted by a single key. As soon as you want to sort by multiple keys, the sorting option flag is ignored. You can pass in callables instead of collection keys, which will do the job, but it requires you to write several callables.Here is my real world example:
I could shorten the code by using
strcasecmp()
but I feel like just using the sorting flags option would be the simplest. It would allow me to reduce the above code to this:This PR supports all the flags currently supported by the different array
sort()
functions. I included tests for each specific sorting flag since they all had to be implemented manually as thesortByMany()
method relies onusort()
.Since the
$options
flag was never passed tosortByMany()
, I didn't see this as a bc issue, so I have targeted the 10.x branch.