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

Sort by more than one column #2887

Closed
cannikin opened this issue Jun 29, 2020 · 2 comments
Closed

Sort by more than one column #2887

cannikin opened this issue Jun 29, 2020 · 2 comments
Labels
kind/feature A request for a new feature.

Comments

@cannikin
Copy link

Problem

The orderBy option does not allow you to specify more than one column to sort by. SQL itself lets you specify as many sorts as you want, and performs the sort as expected: sort records by the first column. If any of those values are the same, sort within those records by the second column, etc.

For example, sorting a list of contacts by last name and then by first name: contacts with the same last name will still be sorted alphabetically:

SELECT firstName
FROM contacts
ORDER BY lastName asc, firstName asc

Billy Bob
Aimee Cameron
Kate Cameron
Rob Cameron
John Doe
...

Suggested solution

Allow orderBy to accept an array of columns/directions to sort by which then passes them onto to SQL:

db.user.findMany({ orderBy: [{lastName: 'asc'}, {firstName: 'asc}] })

Alternatives

Some aspects are impossible to replicate: the results of the sorting are taken into account before returning a LIMIT, whcih you can't do outside of SQL—you'd need to return the entire result set and then sort on the client before knowing which records would be included in the cutoff and not.

If limiting isn't a concern, you could manually write some kind of multi-grouped sort that emulates what SQL already does natively.

Additional context

This is basic functionality provided by SQL, I don't a reason to arbitrarily restrict Prisma from using these features.

@janpio janpio added kind/feature A request for a new feature. team/product labels Jun 30, 2020
@thebiglabasky
Copy link

Thanks @cannikin!
We have this one tracked here as well prisma/prisma-client-js#702
It's on the near term roadmap (that I'm busy putting together so we can publish it, stay tuned!) so something we'll likely support soon (think within 1-3 months, the sooner the better).

@thebiglabasky
Copy link

I'll thus close this issue in favor of this other one.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants