🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to force the order of authors in a frontend using GROQ queries in Sanity.io.

26 replies
Last updated: Jun 17, 2021
hey! I'm iterating over authors, what's the best way to force the order I want on my frontend?
Jun 17, 2021, 1:43 PM
The order returned from your GROQ query should be retained when using
client.fetch()
.
Jun 17, 2021, 2:04 PM
the thing is, how do I set the order in the first place to what I want?
Jun 17, 2021, 2:05 PM
I'm thinking adding like an "order" property but I wanted to know if there's a better way
Jun 17, 2021, 2:05 PM
Ahh, got it. Yes, order() is the way to go.
Jun 17, 2021, 2:08 PM
hmm but I don't have any property to order from right now
Jun 17, 2021, 2:08 PM
I believe the default is
order(_id asc)
, though I wouldn’t expect it to make a difference if you leave it unspecified. Are you finding your order of authors is changing on your front end?
Jun 17, 2021, 2:09 PM
no, I want a specific order
Jun 17, 2021, 2:10 PM
I'm always getting the same order but that's not the one I need for my page
Jun 17, 2021, 2:10 PM
basically, it's the team page and I want to sort it by hire date
Jun 17, 2021, 2:11 PM
What order are you after?
Jun 17, 2021, 2:11 PM
Ahh. Is there a hire date field?
Jun 17, 2021, 2:11 PM
should I just add hire date I guess
Jun 17, 2021, 2:11 PM
as a property
Jun 17, 2021, 2:11 PM
not right now
Jun 17, 2021, 2:11 PM
I was hoping to do it manually but it doesn't seem to be a way of doing that I guess
Jun 17, 2021, 2:11 PM
I believe you could use this plugin if you want to do it manually. The page includes instructions on how to order in GROQ and GraphQL.
Jun 17, 2021, 2:12 PM
This is exactly what I wanted!
Jun 17, 2021, 4:43 PM
I'll try it out and get back to you
Jun 17, 2021, 4:43 PM
update: yes, exactly what I needed, it was super easy to use! thank you
Jun 17, 2021, 4:57 PM
just curious Geoff, is it more recommended to use GROQ or GraphQL?
Jun 17, 2021, 4:58 PM
I think that’ll depend on the tech you’re using and who you ask. I tend to see Next.js users using GROQ and Gatsby users using GraphQL. That said, someone built a plugin to use GROQ with Gatsby and some Next users will use GraphQL because that’s what they know. There are some things that can be done in GraphQL but not GROQ (e.g., recursive dereferencing), but for the most part I find GROQ pairs extremely well with Sanity.
Jun 17, 2021, 5:01 PM
okay gotcha, we're using GROQ with Next now but it's my first time using it
Jun 17, 2021, 5:01 PM
so we'll continue with it
Jun 17, 2021, 5:02 PM
I think you’ll be happy with that.
Jun 17, 2021, 5:02 PM
perfect, thank you for the detailed answer
Jun 17, 2021, 5:02 PM
Feel free to ask any questions here or in the groq channel. There are also some recent streams with
user G
and
user P
about using GROQ (there’s a part 3 but it’s not included in that playlist yet). The docs are also geared toward GROQ users so you’ll find plenty of use cases littered throughout. I consult the cheat sheet daily.
Jun 17, 2021, 5:04 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?