Repository navigation
[10.x] Support sort option flags on sortByMany Collections - #50269
Merged
Merged
Conversation
crynobone
reviewed
Feb 29, 2024
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
|
Hmm, this seems to cause some issues for me: [2024-03-07 20:32:28] local.ERROR: Illuminate\Support\Collection::sortByMany(): Argument #2 ($options) must be of type int, array given, called in /app/vendor/laravel/framework/src/Illuminate/Collections/Collection.php on line 1400 {"userId":1,"exception":"[object] (TypeError(code: 0): Illuminate\\Support\\Collection::sortByMany(): Argument #2 ($options) must be of type int, array given, called in /app/vendor/laravel/framework/src/Illuminate/Collections/Collection.php on line 1400 at /app/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:1434)
[stacktrace]I use something like this: return $this->getMedia('clips')->sortByDesc(
['custom_properties->bitrate', 'desc'],
['custom_properties->height', 'desc'],
['custom_properties->width', 'desc'],
);I think it needs to be defined differently, so I guess an user error? Edit: it should be defined now as: return $this->getMedia('clips')->sortByDesc([
'custom_properties->bitrate',
'custom_properties->height',
'custom_properties->width',
], SORT_DESC);I do like this change! It makes it way more clean and better to work with. :) |
Contributor
Author
return $this->getMedia('clips')->sortByDesc([
'custom_properties->bitrate',
'custom_properties->height',
'custom_properties->width',
]);That is just shorthand for this: return $this->getMedia('clips')->sortBy([
'custom_properties->bitrate',
'custom_properties->height',
'custom_properties->width',
], descending: true);Which I don't think will actually work since the This is the proper, working way to do it: return $this->getMedia('clips')->sortBy([
['custom_properties->bitrate', 'desc'],
['custom_properties->height', 'desc'],
['custom_properties->width', 'desc'],
]); |
|
@TWithers Thanks for providing the proper solution! It seems I just needed to wrap it into an array. :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
$optionsflag was never passed tosortByMany(), I didn't see this as a bc issue, so I have targeted the 10.x branch.