Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix for sortByDesc ignoring multiple attributes #50431

Merged
merged 3 commits into from
Mar 10, 2024

Conversation

TWithers
Copy link
Contributor

@TWithers TWithers commented Mar 9, 2024

When calling sortByDesc() on a collection, it should match the signature of the sortBy() method.

The sortBy() method accepts an array of attributes to sort a collection in 2 different formats:

  1. [['attribute1', 'asc|desc'], ['attribute2', 'asc|desc']]
  2. ['attribute1', 'attribute2'] which is then transformed to [['attribute1', 'asc'], ['attribute2', 'asc']] in the sortByMany() method.

Essentially, when you pass in an array as the first parameter, you need to be explicit about sorting direction, or it will sort in ascending order. Also, when you pass array as the first parameter, it ignores the $descending flag in the sortBy() method.

Currently, the sortByDesc() method just calls sortBy() and sets the $descending flag to true. So when you pass in an array as the first parameter, if you are not specific, it sorts in ascending order.

This PR resolves this issue by converting the array to the more descriptive format and forcing it to sort in descending order.

@taylorotwell taylorotwell merged commit 24d7bd2 into laravel:10.x Mar 10, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants