I have a pretty standard graphql which represent a tree structure:
I would like to make a graph traversal and force the order according to the order
I set on each edge:
A -> C
A -> B -> E
A -> B -> D
I tried to add a SORT on my query, but it sorts the whole resulting array which is not what I want:
FOR v, e, p IN 1..1000 OUTBOUND A
edge_collec
SORT e.order
RETURN v
Is there a way to do this using AQL?
What the query does is:
edge_collec
from start vertex Aorder
The edge attribute e.order
is either 0 or 1:
A --[ order: 1 ]--> B
A --[ order: 0 ]--> C
B --[ order: 1 ]--> D
B --[ order: 0 ]--> E
Sorting by order
will return C and E (0) before B and D (1).
Because two edges have the same value, it's undefined whether C or E is returned first, and whether B or D is returned third.
If you want the vertices at depth = 1 to be returned before the vertices at depth = 2, but still sort by order
on each depth level, you can use:
SORT LENGTH(p.edges), e.order
LENGTH(p.edges)
gives you the current depth of the traversal. It first sorts by depth, then by the edge attribute and will give you the desired result order: C B E D