GraphQL

Tutorial 7 - Sorting

11min
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 query sorted categories { records(per page 2000, filter {table {contains "category"}}) { results { table id properties } } } step 1) first, we'll add in a sort parameter code query sorted categories { records(per page 2000, sort {}) { results { table id properties } } } notes as a parameter, sort is contained within the round brackets after the query type explorer step 2) next, you can add a sort method you can either use 2\) a) a field from the main options code query sorted categories { records(per page 2000, sort {created at {}}) { results { table id properties } } } explorer 2\) b) a property from properties code query sorted categories { records(per page 2000, sort {properties {name "weighting"}}) { results { table 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 step 3) select a sort order you can choose between the keywords desc (descending) and asc (ascending) code query sorted categories { records(per page 2000, sort {properties {name "weighting", order desc}}) { results { table id properties } } } explorer 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 query sorted categories { records(per page 2000, sort { properties \[ {name "weighting", order desc}, {name "release date", order desc} ] }) { results { table 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 you use what you've learned so far to start to build your own application programming interface (api) this will use graphql to query the database, liquid to display the results on an endpoint page and javascript to get those results on the client side