Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →
Skip to content
Sanity
Get started
  • Sanity Studio - Flexible editing environment
  • Content Lake - Real-time database
  • Developer experience - Tooling you love
  • Structured content - The philosophy behind Sanity
  • Review changes - View edits & rollback instantly
  • Image pipeline - On-demand transformations
  • E-commerce - Better shopping experiences
  • Marketing sites - Control your story
  • Products & services - Innovate and automate
  • Mobile apps - Content backend for every OS
  • Morning Brew - Omnichannel media distribution
  • InVision - Delivering exceptional customer experiences
  • DataStax - Personalization for global audience
  • Cloudflare - Flexible modeling for a global CDN
  • React
  • Gatsby
  • Next
  • Nuxt
  • Eleventy
  • Netlify
  • Vercel
  • Algolia
  • Documentation
  • Reference
  • Guides
  • Tools & plugins
  • Project showcase
  • Schemas & snippets
  • Technology partners
  • Get support
  • Share your work
  • Migrating the Netlify Blog from Hugo to Eleventy using Sanity
PricingContact salesLog inGet started
NEWS · June 30th 2021

GROQ pipes with tasty, pricy candy bars

In the latest episode of Kap & Lauren learn GROQ - Part 4, we learned how to use the GROQ pipe operator with the order function to sort our data into ascending or descending order.

Lauren Etheridge

Lo is a Developer Relations Specialist at Sanity.

Kapehe

Kapehe is a Developer Relations Specialist at Sanity.

Kap and I decided to dive into the e-commerce sample data set so that we could query delicious and luxurious candy bars! The GROQ pipe| operator allows you to grab the data from the left-hand side of the array and pass it over to the right-side pipeline component for more processing. In our case, we are passing data over to the order function order() to sort products by title and price.

From our test studio, loaded with candy bar product data (shoutout to Wasabi Kit Kats, our favorite!) we made the following query:

*[_type == "product"] | order(
	defaultProductVariant.price desc, title asc)[0...3]{
	  title,
	  "luxuryItems" : defaultProductVariant{
		  price
	}
}

This query filters on the document type product and pulls out the title and price only using a projection. Notice the defaultProductVariant.price, what's happening there? Did a cyborg named defaultProductVariant capture all of the candy bar prices?!? It sure seems like it, but really price is a field inside of the defaultProductVariant object, so it must remain attached to the price or any other field that is inside of it.

The GROQ pipe | operator grabs this data, thanks to the filter for its cooperation and hard work, and passes the data to the order function. The order function sorts the data by the parameters Kap and I set. First, the product data is sorted by price in descending order, and then it is sorted again by title in ascending order. We are also slicing [0...3] the data and using an alias luxuryItems in this query because we only want to return the 3 most expensive products as our luxury candy bars.

If you missed the stream, you can watch Kap & Lauren learn GROQ - Part 4 on YouTube. Follow us on Twitch and tune in for another episode of Kap & Lauren learn GROQ, we hope to see you there!

Platform

Structured ContentDeveloper experienceContent LakeSanity StudioSecurity & Compliance

Resources

Documentation
  • Content Modeling
  • React Blog
  • Gatsby Blog
  • Next.js Landing Pages
  • Progressive Web Application
  • Single Page Application
  • Svelte & Typescript App
  • Vue & Tailwind Blog
  • Developer Portfolio Templates
  • Form validation with Yup
  • Live Preview with Next.js and Sanity.io
  • React Starters
  • Next.js Starters
  • Next.js Landing Pages
  • Next.js E-commerce
  • Gatsby Starters
  • Gatsby Plugins
  • Developer Portfolio Templates
Case Studies
  • Headless CMS
  • Digital Experience Platform
  • Static Site Generator
  • Localization
  • GraphQL vs REST
  • React CMS
  • Next.JS CMS
  • Gatsby CMS
  • Node CMS
  • E-commerce CMS
  • Vue CMS
  • Angular CMS
  • GraphQL CMS
  • Newspaper CMS
  • Magazine CMS

Company

Contact SalesEnterpriseCareersTerms of Service

Stay connected

  • Github
  • Slack
  • Twitter
  • YouTube
  • Stack Overflow
  • Blog RSS

Subscribe to our newsletter

©Sanity 2022