neo4j cypher keep ordering imposed by path for later in the query

Viewed 406

I am using a query like

MATCH p=((:Start)-[:NEXT*..100]->(n))
WHERE ALL(node IN nodes(p) WHERE ...)
WITH DISTINCT n WHERE (n:RELEVANT)
...
RETURN n.someprop;

Where I want to have the results ordered by the natural ordering arising from the direction of the -[:NEXT]-> relationships. But the WITH in the third line scrambles up that ordering. Problem is, I need the with to 1. filter for :RELEVANT nodes and 2. to get only distinct such nodes.

Is there some way to preserve the ordering? Maybe assign number ordering on the path and reuse it later with ORDER BY? No idea how to do it.

2 Answers
Related