Neo4j 简明教程
Neo4j - Order By Clause
您可以使用 ORDER BY 子句按顺序排列结果数据。
You can arrange the result data in order using the ORDER BY clause.
Syntax
以下是 ORDER BY 从句的语法。
Following is the syntax of the ORDER BY clause.
MATCH (n)
RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
Example
在继续举例之前,请按照以下指示在 Neo4j 数据库中创建 5 个节点。
Before proceeding with the example, create 5 nodes in Neo4j database as shown below.
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"})
以下是一个使用 ORDERBY 子句按球员得分顺序返回上述创建节点的示例 Cypher 查询。
Following is a sample Cypher Query which returns the above created nodes in the order of the runs scored by the player using 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 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Ordering Nodes by Multiple Properties
您可以使用 ORDEYBY 子句按多个属性整理节点。
You can arrange the nodes based on multiple properties using ORDEYBY clause.
Syntax
以下是使用 ORDERBY 子句按多个属性整理节点的语法。
Following is the syntax to arrange nodes by multiple properties using the ORDERBY clause.
MATCH (n)
RETURN n
ORDER BY n.age, n.name
Example
以下是一个示例 Cypher 查询,该查询按以下属性对本章前面创建的节点进行整理:得分和国家。
Following is a sample Cypher Query which arranges the nodes created earlier in this chapter based on the properties - runs and country.
MATCH (n)
RETURN n.name, n.runs, n.country
ORDER BY n.runs, n.country
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Ordering Nodes by Descending Order
您可以使用 ORDERBY 子句按降序排列数据库中的节点。
You can arrange the nodes in a database in a descending order using 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
以下是一个示例 Cypher 查询,该查询使用 ORDERBY 子句按降序排列数据库中的节点。
Following is a sample Cypher Query which arranges the nodes in a database in a descending order using the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
