Skip to content
👀 See Sanity in action: Watch product demo now →
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
  • Aether - Unique digital shopping experience
  • Morning Brew - Omnichannel media distribution
  • InVision - Delivering exceptional customer experiences
  • DataStax - Personalization for global audience
  • React
  • Gatsby
  • Next
  • Nuxt
  • Eleventy
  • Netlify
  • Vercel
  • Algolia
  • Documentation
  • Reference
  • Guides
  • Resource library
  • Headless CMS
  • Tools & plugins
  • Project showcase
  • Schemas & snippets
  • Agency partners
  • Technology partners
  • Get support
  • Share your work
  • Enterprise SSO
EnterprisePricing
Contact salesLog inGet started
Published 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.

Lo Etheridge

Developer Relations and Strategic Community DEI Efforts at Sanity.io

Kapehe

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
  • Sanity vs Contentful
  • Sanity vs Strapi
  • Sanity vs Adobe Experience Manager
  • Sanity vs Hygraph
  • Sanity vs Sitecore
  • Sanity vs Storyblok
  • Sanity vs Contentstack
  • Sanity vs Prismic
  • Sanity vs Drupal

Resources

Documentation
  • 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
Resource library
  • Agency partners
  • Technology partners
  • Blog Template
  • Personal Website Template
  • Developer Portfolio Templates
  • All Templates
Case Studies
  • Headless CMS
  • What is an API CMS
  • Static Sites 101
  • Headless SEO
  • Localization
  • GraphQL vs REST
  • What is a DXP?
  • Typescript 101
  • React CMS
  • Next.JS CMS
  • CMS for Shopify
  • Content platform
  • Multilingual CMS
  • Static Site CMS
  • Gatsby CMS
  • Node CMS
  • E-commerce CMS
  • Vue CMS
  • Angular CMS
  • GraphQL CMS
  • Newspaper CMS
  • Magazine CMS
  • Mobile apps CMS

Company

Contact SalesEnterpriseCareersTerms of ServiceAccessibility Statement

Stay connected

  • GitHub
  • Slack
  • Twitter
  • YouTube
  • Stack Overflow
  • Blog RSS
  • Newsletter
©Sanity 2023