Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Are we able to sort by a related document's field with the REST API?

default discord avatar
allannnclast year
3

Hey! I'm looking to do a REST query like


/api/ticket/${qs.stringify(
              {
                where: {
                  user: {
                    equals: currentUser.id,
                  },
                },
                sort: 'event.startsAt',
                depth: 1, // get event data too
              },
              { addQueryPrefix: true }
            )}

But it doesn't seem like sorting by a relationship's nested field is possible. So I've gone with a frontend .sort() after querying instead



Just want to confirm that my understanding is correct here (we can't sort by a related doc's field)

  • default discord avatar
    notchrlast year

    @allannnc I'm not sure about doing this with builtins, but it may exist. Alternatively, you could add an entry to the collections "endpoints" array so that you can manually sort the data and send it back

  • default discord avatar
    allannnclast year

    Thanks for the reply! I think I'll stick with the frontend sort as it ends up being the same amount of work anyway

  • default discord avatar
    notchrlast year

    That's fair! If it were a large amount of items, I would say it's faster on the backend, otherwise a frontend sort works!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.