概述
ORDER BY子句调整别名代表的数据行的顺序。
语法
ORDER BY <expression> <string>, <expression> <string>, ...
<expression>
是排序依据;如果使用了多个别名,它们必须同源,此时按照从左到右的顺序逐级进行排序<string>
是排序方式,支持ASC
(升序)或DESC
(降序),大小写不敏感,忽略时为ASC
例如,对以下路径查询产生的数据流进行多重排序。先按照终点n的半径大小降序排列,半径大小相同的再按照终点n的_uuid
大小升序排列:

n([4, 2]).e().n(as n) as path
order by n.radius desc, n
return path
示例
示例图集

在一个空图集中,依次运行以下各行语句创建示例图集:
create().node_schema("student").node_schema("course")
create().node_property(@*, "name").node_property(@student, "age", int32).node_property(@course, "credit", int32)
insert().into(@student).nodes([{_id:"S001", _uuid:1, name:"Jason", age:25}, {_id:"S002", _uuid:2, name:"Lina", age:23}, {_id:"S003", _uuid:3, name:"Eric", age:24}, {_id:"S004", _uuid:4, name:"Emma", age:26}, {_id:"S005", _uuid:5, name:"Pepe", age:24}])
insert().into(@course).nodes([{_id:"C001", _uuid:6, name:"French", credit:4}, {_id:"C002", _uuid:7, name:"Math", credit:5}])
insert().into(@default).edges([{_uuid:1, _from_uuid:1, _to_uuid:6}, {_uuid:2, _from_uuid:2, _to_uuid:6}, {_uuid:3, _from_uuid:3, _to_uuid:6}, {_uuid:4, _from_uuid:2, _to_uuid:7}, {_uuid:5, _from_uuid:3, _to_uuid:7}, {_uuid:6, _from_uuid:4, _to_uuid:7}, {_uuid:7, _from_uuid:5, _to_uuid:7}])
分组+排序
本例查询“课程-[]-学生”1步路径,按课程分组后,统计各组内学生的数量,再按学生数量降序排列:
n({@course} as a).e().n({@student})
group by a
with count(a) as b
order by b desc
return table(a.name, b)
| a.name | b |
|--------|---|
| Math | 4 |
| French | 3 |
多级排序
本例查询“课程-[]-学生”1步路径,按课程的credit属性升序排列后,再按学生的age属性降序排列,返回返回路径中点和边的全部信息:
n({@course} as a).e().n({@student} as b) as p
order by a.credit, b.age desc
return p{*}
French <---- Jason
French <---- Eric
French <---- Lina
Math <---- Emma
Math <---- Eric
Math <---- Pepe
Math <---- Lina