Add way to specify secondary sort on unique column #439
Unanswered
trevorgehman
asked this question in
Ideas
Replies: 3 comments
-
Thinking more about this:
Would you be open to a PR for this functionality? We could explain in the sorting docs when you would want to use this. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Can't this be solved with 1 custom sort as well? Extending ->allowedSorts([
AllowedSort::custom('comments_count', new DeterministicSort('id')),
]) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
One issue with sorting on a SQL column that's not unique, such as
first_name
, is that the order of returned rows isn't predictable if there are duplicate values. This can cause issues with pagination, where the same row is returned on multiple pages, even without any data changing in the database.I've seen this happen frequently when sorting on relationship counts, where a lot of posts have
comments_count: 0
for example.Solution
It would be nice to have a way to always apply a secondary sort on a unique column. So if the http query is
?sort=-name
then the database query looks like this:Currently I'm just creating a bunch of custom sorts, but it would be nice to have something like:
Alternatives
I've thought about manually adding the sort:
The problem with doing that is that I'd need to specify the sort direction, and SQL performance isn't great if the sort directions are different when you're sorting on multiple columns. Here's more information on that:
Beta Was this translation helpful? Give feedback.
All reactions