You can set up the end result facts in order the use of the ORDER BY clause.
Syntax
Following is the syntax of the ORDER BY clause.
MATCH (n)
RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
Example
Before intending with the example, create 5 nodes in Neo4j database as proven beneath.
CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
Following is a pattern Cypher Query which returns the above created nodes in the order of the runs scored by the participant the use of the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j laptop App and begin the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as proven inside the following screenshot.
Step 2 − Copy and paste the preferred query within the dollar prompt and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you will get the subsequent result.
Ordering Nodes by way of Multiple Properties
You can set up the nodes based totally on multiple homes the usage of ORDEYBY clause.
Syntax
Following is the syntax to arrange nodes by means of more than one residences using the ORDERBY clause.
MATCH (n)
RETURN n
ORDER BY n.age, n.name
Example
Following is a sample Cypher Query which arranges the nodes created in advance in this chapter primarily based on the properties - runs and usa.
MATCH (n)
RETURN n.name, n.runs, n.country
ORDER BY n.runs, n.country
To execute the above query, perform the subsequent steps −
Step 1 − Open the Neo4j computing device App and begin the Neo4j Server. Open the built-in browser app of Neo4j the use of the URL http://localhost:7474/ as proven in the following screenshot.
Step 2 − Copy and paste the desired query in the greenback prompt and press the play button (to execute the query) highlighted in the following screenshot.
Result
On executing, you may get the subsequent result.
Ordering Nodes with the aid of Descending Order
You can arrange the nodes in a database in a descending order the use of the ORDERBY clause.
Syntax
Following is the syntax to arrange the nodes in a database.
MATCH (n)
RETURN n
ORDER BY n.name DESC
Example
Following is a pattern Cypher Query which arranges the nodes in a database in a descending order the use of the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computing device App and begin the Neo4j Server. Open the integrated browser app of Neo4j the use of the URL http://localhost:7474/ as proven in the following screenshot.
Step 2 − Copy and paste the desired query in the greenback prompt and press the play button (to execute the question) highlighted in the following screenshot.
Result
On executing, you'll get the subsequent end result.