matthiash
matthiash

Reputation: 3235

In neo4j, how to sort multiple lists?

In neo4j, how do you order elements in lists? More specifically, I have rows of lists as input, and for each list, I want to return the ordered list. Thus the query

WITH [[6,2,1],[5,4,5]] as ls
UNWIND ls as l
RETURN l

which now returns

[6, 2, 1]
[5, 4, 5]

should be modified to return

[1, 2, 6]
[4, 5, 5]

(The query should obviously be scalable to a large number of lists)

Upvotes: 0

Views: 189

Answers (1)

logisima
logisima

Reputation: 7458

You can do it in plain cypher like that :

WITH [[6,2,1],[5,4,5]] as tabOftabs
UNWIND range(0, size(tabOftabs)-1, 1) as tabIndex
UNWIND tabOftabs[tabIndex] as tabElement
WITH tabIndex,  tabElement  ORDER BY tabIndex, tabElement
WITH tabIndex, collect(tabElement) AS result
RETURN result AS l

Or you can use an APOC function :

WITH [[6,2,1],[5,4,5]] as ls
UNWIND ls as l
RETURN apoc.coll.sort(l) AS l

Upvotes: 2

Related Questions