AQL implements a default sort order, however, you can override the default and specify any other sort order using fields in your output by adding the .sort
element to the end of your query as follows:
.sort({"<$asc | $desc>" : ["<field1>", "<field2>",... ]})
Note
You can only specify sorting on fields that are displayed in the output (whether they are those displayed by default or due to a .include
element).
Here are some examples:
// Find all the jars in artifactory and sort them by repo and name items.find({"name" : {"$match":"*.jar"}}).sort({"$asc" : ["repo","name"]}) // Find all the jars in artifactory and their properties, then sort them by repo and name items.find({"name" : {"$match":"*.jar"}}).include("@").sort({"$asc" : ["repo","name"]})
Limitation
The .sort
element has the following limitation:
If your query has an
include
element, you can only specify fields from the primary domain in it.
For example, in the following query, .sort
will not work because the primary domain is an item
, but the include
element specifies that fields from the artifact
, module
, and build
domains should be displayed:
items.find().include("artifact","artifact.module","artifact.module.build")
This means that if you search for an item and include the property you will not be able to sort by the property.
items.find({"repo":"example-repo-local"}).include("repo","path", "name", "created", "@build.number")