Order Your Results in Javascript

There are a couple of query options that give you control over how your results are ordered. You can use the "orderings" option to sort your query results in a particular order. The "after" option allows you to only show documents before or after a certain document in the results.

orderings

The "orderings" option orders the results by the specified field(s). You can specify as many fields as you want.

Simply use the orderings parameter in your query options as shown in the examples below.

Order by a field - lowest to highest

You can use the "orderings" option to order your results by a certain field. By default it will sort the field from the lowest value to the highest.

The following examples shows how to sort the results by the price field from lowest to highest.

Copy
// Order by price of product from lowest to highest
{ orderings : '[my.product.price]' }

Order by a field - highest to lowest

To sort a field from highest to lowest, add "desc" next to the field name. The following shows an example of this.

Copy
// Order by price of product from highest to lowest
{ orderings : '[my.product.price desc]' }

Order by Multiple Fields

You can specify multiple fields to sort the order of your results by. The results will ordered first by the first field specified. If any of those values are equal, then those results will be sorted by the next specified field. And so on.

Here is an example that will order the results first by the price of the products from low to high. It will then sort any products that have the same price by their title.

Copy
// Order first by price, then by title 
{ orderings : '[my.product.price, my.product.title]' }

Order by First or Last Publication Dates

It is also possible to order documents by their first or last publication dates.

The first publication date is the date that the document was originally published for the first time.

Copy
// Order by first publication date from oldest to most recent
{ orderings : '[document.first_publication_date]' }

The last publication date is the most recent date that the document has been published after editing.

Copy
// Order by last publication date from most recent to oldest
{ orderings : '[document.last_publication_date desc]' }

after

The after option can be used along with the orderings option. It will remove all the documents except for those after the specified document in the list.

To clarify, let’s say you have a query that return the following documents in this order:

V9Zt3icAAAl8Uzob (Page 1)
PqZtvCcAALuRUzmO (Page 2)
VkRmhykAAFA6PoBj (Page 3)
V4Fs8rDbAAH9Pfow (Page 4)
G8ZtxQhAALuSix6R (Page 5)
Ww9yuAvdAhl87wh6 (Page 6)

If you add the after option and specify page 3, “VkRmhykAAFA6PoBj”, your query will return the following:

V4Fs8rDbAAH9Pfow (Page 4)
G8ZtxQhAALuSix6R (Page 5)
Ww9yuAvdAhl87wh6 (Page 6)

By reversing the orderings in your query, you can use this same method to retrieve all the documents before the specified document.

This option is useful when creating a navigation for a blog.

Simply use the after parameter in your query options as shown below.

Copy
{ after : 'VkRmhykAAFA6PoBj' }