VATSAL JAIN
VATSAL JAIN

Reputation: 581

Sort based on collect value in neo4j

I am having this query MATCH (r:Class {Name: 'A'})-[perm]->(res:source) RETURN res.Name AS resource, collect(type(perm)) AS permission ORDER BY resource

It returns me a response like this

"AB sources",
      [
        "D",
        "C",
        "A",
        "B"
      ]

What I want is that this D C A B should be sorted like A B C D

I tried putting order by for perm but that gives error. How can I achieve this?

Upvotes: 0

Views: 21

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You'll have to use the APOC library to sort the collection:

MATCH (r:Class {Name: 'A'})-[perm]->(res:source) 
WITH res.Name AS resource, collect(type(perm)) AS permission
RETURN resource, apoc.coll.sort(permission) AS permission
ORDER BY resource

Upvotes: 1

Related Questions