3.3.8. ORDER BY

ORDER BY is a sub-clause following RETURN or WITH, and it specifies that the output should be sorted and how.

3.3.8.1. Introduction

Note that you cannot sort on nodes or relationships, just on properties on these. ORDER BY relies on comparisons to sort the output, see Section 3.2.5.10, “Ordering and comparison of values”.

In terms of scope of variables, ORDER BY follows special rules, depending on if the projecting RETURN or WITH clause is either aggregating or DISTINCT. If it is an aggregating or DISTINCT projection, only the variables available in the projection are available. If the projection does not alter the output cardinality (which aggregation and DISTINCT do), variables available from before the projecting clause are also available. When the projection clause shadows already existing variables, only the new variables are available.

Lastly, it is not allowed to use aggregating expressions in the ORDER BY sub-clause if they are not also listed in the projecting clause. This last rule is to make sure that ORDER BY does not change the results, only the order of them.

Figure 3.14. Graph
alt

3.3.8.2. Order nodes by property

ORDER BY is used to sort the output.

Query. 

MATCH (n)
RETURN n.name, n.age
ORDER BY n.name

The nodes are returned, sorted by their name.

Table 3.97. Result
n.name n.age

3 rows

"A"

34

"B"

34

"C"

32

Try this query live.  CREATE (a {name: 'A', age: 34, length: 170}), (b {name: 'B', age: 34}), (c {name: 'C', age: 32, length: 185}), (a)-[:KNOWS]->(b), (b)-[:KNOWS]->(c) MATCH (n) RETURN n.name, n.age ORDER BY n.name

3.3.8.3. Order nodes by multiple properties

You can order by multiple properties by stating each variable in the ORDER BY clause. Cypher will sort the result by the first variable listed, and for equals values, go to the next property in the ORDER BY clause, and so on.

Query. 

MATCH (n)
RETURN n.name, n.age
ORDER BY n.age, n.name

This returns the nodes, sorted first by their age, and then by their name.

Table 3.98. Result
n.name n.age

3 rows

"C"

32

"A"

34

"B"

34

Try this query live.  CREATE (a {name: 'A', age: 34, length: 170}), (b {name: 'B', age: 34}), (c {name: 'C', age: 32, length: 185}), (a)-[:KNOWS]->(b), (b)-[:KNOWS]->(c) MATCH (n) RETURN n.name, n.age ORDER BY n.age, n.name

3.3.8.4. Order nodes in descending order

By adding DESC[ENDING] after the variable to sort on, the sort will be done in reverse order.

Query. 

MATCH (n)
RETURN n.name, n.age
ORDER BY n.name DESC

The example returns the nodes, sorted by their name in reverse order.

Table 3.99. Result
n.name n.age

3 rows

"C"

32

"B"

34

"A"

34

Try this query live.  CREATE (a {name: 'A', age: 34, length: 170}), (b {name: 'B', age: 34}), (c {name: 'C', age: 32, length: 185}), (a)-[:KNOWS]->(b), (b)-[:KNOWS]->(c) MATCH (n) RETURN n.name, n.age ORDER BY n.name DESC

3.3.8.5. Ordering null

When sorting the result set, null will always come at the end of the result set for ascending sorting, and first when doing descending sort.

Query. 

MATCH (n)
RETURN n.length, n.name, n.age
ORDER BY n.length

The nodes are returned sorted by the length property, with a node without that property last.

Table 3.100. Result
n.length n.name n.age

3 rows

170

"A"

34

185

"C"

32

<null>

"B"

34

Try this query live.  CREATE (a {name: 'A', age: 34, length: 170}), (b {name: 'B', age: 34}), (c {name: 'C', age: 32, length: 185}), (a)-[:KNOWS]->(b), (b)-[:KNOWS]->(c) MATCH (n) RETURN n.length, n.name, n.age ORDER BY n.length