apoc.coll.sortNodes

Function APOC Core

apoc.coll.sortNodes([nodes], 'name') sort nodes by property

Signature

apoc.coll.sortNodes(coll :: LIST? OF NODE?, prop :: STRING?) :: (LIST? OF ANY?)

Input parameters

Name Type Default

coll

LIST? OF NODE?

null

prop

STRING?

null

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (TomH:Person {name:'Tom Hanks', born:1956})
CREATE (TomT:Person {name:'Tom Tykwer', born:1965});

The following sorts a collection of nodes by the name property in descending order:

MATCH (person:Person)
WITH collect(person) AS people
RETURN apoc.coll.sortNodes(people, 'name') AS output;
Table 1. Results
Output

[(:Person {name: "Tom Tykwer", born: 1965}), (:Person {name: "Tom Hanks", born: 1956}), (:Person {name: "Keanu Reeves", born: 1964})]

The following sorts a collection of nodes by the name property in ascending order:

MATCH (person:Person)
WITH collect(person) AS people
RETURN apoc.coll.sortNodes(people, 'name') AS output;
Table 2. Results
Output

[(:Person {name: "Keanu Reeves", born: 1964}), (:Person {name: "Tom Hanks", born: 1956}), (:Person {name: "Tom Tykwer", born: 1965})]