9
votes

In cypher query i have multiple result which i get using collect now how i can order by collect property in cypher?

MATCH(u:User) 
WITH COLLECT({name:u.name,date:u.date}) AS userinfo 
RETURN userinfo

OR in the case there are multiple collections that have been merged

MATCH(u:User)-[r:CreatedBy]->(p:Project) 
WITH COLLECT({name:p.name,date:p.date}) AS info 
MATCH(i:Institue)-[owner:Owner]->(i:Institute) 
WITH COLLECT({instituteName:i.name,date:i.date}) AS instituteinfo,info 
WITH COLLECT(instituteinfo + info) AS alldata 
RETURN alldata
1
Is your goal is to order the items in the collection?Dave Bennett
yes @Dave BennetRitesh Tiwari
Suppose i have multiple collection and i merge all collection then how can i order by it like : MATCH(u:User)-[r:CreatedBy]->(p:Project) WITH COLLECT({name:p.name,date:p.date}) AS info MATCH(i:Institue)-[owner:Owner]->(i:Institute) WITH COLLECT({instituteName:i.name,date:i.date}) AS instituteinfo,info WITH COLLECT(instituteinfo + info) AS alldata RETURN alldata in this query i want order by dateRitesh Tiwari

1 Answers

5
votes

You simply need to order the user nodes by the attribute of your choice prior to collecting them. Something like this..,

MATCH(u:User)
WITH u
ORDER BY u.name
WITH COLLECT({name:u.name,date:u.date}) AS userinfo 
RETURN userinfo

Or if you were looking to combine multiple collections and produce a single ordered collection you could recombine them something like this...

MATCH(u:User)-[r:CreatedBy]->(p:Project) 
WITH COLLECT({name:p.name, date:p.date}) AS info 
MATCH(i:Institue)-[owner:Owner]->(i:Institute) 
WITH COLLECT({instituteName:i.name, date:i.date}) AS instituteinfo,info 
WITH instituteinfo + info AS alldata 
UNWIND alldata as node
WITH node
ORDER BY node.name
WITH COLLECT (DISTINCT node) as alldata
RETURN alldata