apoc.coll.sortNodes

Details

Syntax

apoc.coll.sortNodes(coll, prop)

Description

Sorts the given LIST<NODE> by the property of the nodes into descending order.

Arguments

Name

Type

Description

coll

LIST<NODE>

The list of nodes to be sorted.

prop

STRING

The property key on the node to be used to sort the list by.

Returns

LIST<ANY>

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 examples sort a collection of nodes by the name property in descending order using both APOC and Cypher:

  • apoc.coll.sortNodes
  • Using Cypher’s COLLECT subquery
apoc.coll.sortNodes
MATCH (person:Person)
WITH collect(person) AS people
RETURN apoc.coll.sortNodes(people, 'name') AS output
Using Cypher’s COLLECT subquery
RETURN COLLECT {
    MATCH (person:Person)
    RETURN person ORDER BY person.name DESC
} AS output
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:

  • apoc.coll.sortNodes
  • Using Cypher’s COLLECT subquery
apoc.coll.sortNodes
MATCH (person:Person)
WITH collect(person) AS people
RETURN apoc.coll.sortNodes(people, '^name') AS output
Using Cypher’s COLLECT subquery
RETURN COLLECT {
    MATCH (person:Person)
    RETURN person ORDER BY person.name
} AS output
Results
output

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