1

I need to sort a collection, then do other stuff. Here is what the start of my query looks like:

match ()-[r]-(n)-[s:STATE]->()
// order by s.from ??
with collect(s) as states
// do stuff

Is there a way to use ORDER BY with WITH or does it only work with RETURN ?

See also: this and this other question

Community
  • 1
  • 1
Renaud
  • 4,569
  • 7
  • 41
  • 72
  • What is the "other stuff"? What are you trying to accomplish with this? Usually I think of sorting as something that happens to a result set - and in any case, if you use collect() aren't you creating a collection? Typically in "java thinking" collections come both ordered and unordered. I don't see anything in the neo4j docs that guarantee collections are ordered...so it might be unsafe to do something like "create a collection after sorting". If the collection is un-ordered, the sort might not make sense.... – FrobberOfBits Oct 08 '14 at 13:45
  • I want to filter on the condition: `s[n].to = s[n+1].from` for successive elements in the collection. This relies on a notion of ordering within the collection. – Renaud Oct 08 '14 at 13:47
  • 1
    It's a good question. I'm stumped, not sure ordering a collection in that way is possible. Not sure if it is possible, if it's wise to do (i.e. if cypher doesn't guarantee collection ordering). Hoping I'm wrong -- let's see what others come up with. – FrobberOfBits Oct 08 '14 at 14:11
  • I didn't know cypher didn't guarantee collection ordering. It seems counter intuitive since you can sort rows... – Renaud Oct 08 '14 at 14:33
  • I am actually not sure on that point, we need a neo4j dev to clarify that. I'm only pointing out that in Java (which neo is based on) the term collection does not guarantee or imply any ordering. I'd consider it unwise to assume an ordering unless you can point to docs that say it's guaranteed. I looked, and I can't find any such docs. – FrobberOfBits Oct 08 '14 at 14:50

0 Answers0