website logo
⌘K
Introduction
PORTAL
Agency Whitelabelling
PAGES
Pages - Accessing Page Data
Pagination Layouts
Page Templates
FORMS
CATEGORIES
Filtering WebApps and Modules by Categories
Outputting Categories on WebApp / Module / eCommerce Layouts
Outputting Category Fields in any Location
Category Detail Layouts
FAQ - Categories
COMPANY INFORMATION
Company Information
SITE SEARCH
Site Search
PUBLIC API/ZAPIER
Zapier - Formatting arrays correctly
Public API/Zapier Changelog
MODULES
Module Marketplace
Building Custom Modules
Siteglide Modules
Front-end Submit Modules
DATA STRUCTURES
Creating WebApps from the CLI
Field Types
WEBAPPS
Front-end Submit WebApps
Layouts
Search and Filtering
Understanding Custom Field Names and IDs
FAQ - WebApps
CRM
User Details
User Secure Zones
Storing User's Favourite WebApp / Module Items
User's Form Submissions (Cases)
How Users Edit their Email and Password Front End
Editing a User's CRM record Front End with Custom Field Sets
CLI
Introducing Siteglide CLI
CLI Changelog
Secure Zones with CLI
Page Templates with Siteglide CLI
Pages with Siteglide CLI
Includes with Siteglide CLI
Managing Email Templates
Migrate - Manual Form setup
Migrate - Convert existing Forms
Liquid
Accessing Assets
Liquid Dot Notation
Using WebApp Collections- Tutorial
Using the current_user object with Secure Zones
Preventing Duplicate Content with Query Parameters- Canonical URL and Robots.txt
FAQ - Liquid
GraphQL
Tutorial Overview
About GraphQL
Tutorial 1- Your First Query
Tutorial 2 - Pagination
Tutorial 3 - Filtering the Results
Tutorial 3 - Answers to the First Filtering Challenge
Tutorial 4 - Advanced Filtering
Tutorial 4 - Challenge Answers
Tutorial 5 - Using Liquid to run GraphQL queries on your Site
Tutorial 6 - Variables
Tutorial 6 - Answers to the Variables Challenge
Tutorial 7 - Sorting
Tutorial 8 - Building a Liquid API GET Endpoint Page powered by GraphQL queries
Best Practice and Performance
Module/WebApp Caching
Getting Started with Liquid Caching - to Reduce Server Response time and Improve Performance
Includes
ecommerce/checkout_standard
Frequently Asked Questions
How do I control Timezones?
Using Liquid Logic to Check if a field exists, or is empty on the Front End
How do I learn more about Liquid?
How to setup a multi domain start page
Docs powered by
Archbee
GraphQL

Tutorial 7 - Sorting

15min

You can change the type and order of sorting. You can also sort by multiple properties at once.

Introduction

In this Article, we'll look at how you can instruct the query to return items in an order of your choice. Using sort along with pagination means you can make sure the first pagen of results shows you the items you're interested in. Using sort along with variables allows you to let your end-User customise the sort order themselves.

Sorting by a Single Property

In this example, we'll start with a query which fetches the Categories.

Code:

GraphQL
|
query sorted_categories {
  models(per_page: 2000, filter: {model_schema_name: {contains: "category"}}) {
    results {
      model_schema_name
      id
      properties
    }
  }
}


Step 1) First, we'll add in a sort parameter

Code:

GraphQL
|
query sorted_categories {

  models(per_page: 2000, sort: {}) {

    results {

      model_schema_name

      id

      properties

    }

  }

}


Notes:

  • As a parameter, sort is contained within the round brackets after the query type

Explorer:

Document image


Step 2) Next, you can add a sort method.

You can either use:

2) a) A field from the main options

Code:

GraphQL
|
query sorted_categories {

  models(per_page: 2000, sort: {created_at: {}}) {

    results {

      model_schema_name

      id

      properties

    }

  }

}


Explorer:

Document image


2) b) a property from properties

Code:

GraphQL
|
query sorted_categories {

  models(per_page: 2000, sort: {properties: {name: "weighting"}}) {

    results {

      model_schema_name

      id

      properties

    }

  }

}


Notes:

  • Remember, any field which is not a Platform default field will be a property. This means any field not listed in the other options, including several Siteglide fields such as "weighting". Default fields tend to be like created_at automatically filled in when an item is created.

Explorer:

Document image


Step 3) Select a sort order

You can choose between the keywords DESC (descending) and ASC (ascending).

Code:

GraphQL
|
query sorted_categories {

  models(per_page: 2000, sort: {properties: {name: "weighting", order: DESC}}) {

    results {

      model_schema_name

      id

      properties

    }

  }

}


Explorer:

Document image


Sorting by Multiple Properties

Sometimes you may want to initially sort by one property, e.g. weighting, but then some items may have a blank weighting property. Those items where the sort field is blank will go to the end of the results list, but you can add additional sort conditions to sort these further.

Let's say we wish to firstly sort by "weighting", then sort those items without a weighting by the "release date".

Code:

GraphQL
|
query sorted_categories {

  models(per_page: 2000, sort: {

    properties: [

      {name: "weighting", order: DESC}, 
      {name: "release_date", order: DESC}

    ]

  }) {

    results {

      model_schema_name

      id

      properties

    }

  }

}


Notes:

  • Here we use an array with square brackets [] to add multiple sorting objects with curly braces {}
  • Unfortunately, arrays are not yet supported by the Explorer, so you'll need to add this code manually.
  • You can use different sort orders for each condition

Next Time

If you want to challenge yourself, you could choose to try making a query which uses variables to change the order and field by which results are sorted.

Next time, we'll look at how to implement keyword searches in your query. To do this, we'll look at a new type of query "customizations" instead of "models".

Updated 03 Mar 2023
Did this page help you?
PREVIOUS
Tutorial 6 - Answers to the Variables Challenge
NEXT
Tutorial 8 - Building a Liquid API GET Endpoint Page powered by GraphQL queries
Docs powered by
Archbee
TABLE OF CONTENTS
Introduction
Sorting by a Single Property
Step 1) First, we'll add in a sort parameter
Step 2) Next, you can add a sort method.
2) a) A field from the main options
2) b) a property from properties
Step 3) Select a sort order
Sorting by Multiple Properties
Next Time
Docs powered by
Archbee