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
You can order by multiple properties by stating each variable in the ORDER BY clause. Cypher will sort the result by the first variable listed, and for equals values, go to the next property in the ORDER BY clause, and so on. This returns the nodes, sorted first by their age, and then by their name.
The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.
The Cypher query offers aggregation similar to GROUP BY offered by SQL. The aggregate function can take multiple values and can calculate the aggregated values for them. In this recipe, we will learn the common aggregation techniques, with the help of examples.
UNWIND expands a list into a sequence of rows.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With