apoc.coll.sortMaps

Details

Syntax

apoc.coll.sortMaps(list, prop)

Description

Sorts the given LIST<MAP<STRING, ANY>> into descending order, based on the MAP property indicated by prop.

Arguments

Name

Type

Description

list

LIST<MAP>

The list of maps to be sorted.

prop

STRING

The property key to be used to sort the list of maps by.

Returns

LIST<ANY>

Usage examples

The following examples sort a list of maps in reverse alphabetical order by the key name using both APOC and Cypher:

apoc.coll.sortMaps
WITH [
    {name: "Lionel Messi"},
    {name: "Cristiano Ronaldo"},
    {name: "Wayne Rooney"}
] AS list
RETURN apoc.coll.sortMaps(list, "name") AS output
Using Cypher’s COLLECT subquery
WITH [
    {name: "Lionel Messi"},
    {name: "Cristiano Ronaldo"},
    {name: "Wayne Rooney"}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name DESC
} AS output
Results
output

[ { "name": "Wayne Rooney" } , { "name": "Lionel Messi" } , { "name": "Cristiano Ronaldo" } ]

The following examples sort a list of maps in alphabetical order by the key name using both APOC and Cypher:

apoc.coll.sortMaps
WITH [
    {name: "Lionel Messi"},
    {name: "Cristiano Ronaldo"},
    {name: "Wayne Rooney"}
] AS list
RETURN apoc.coll.sortMaps(list, "^name") AS output
Using Cypher’s COLLECT subquery
WITH [
    {name: "Lionel Messi"},
    {name: "Cristiano Ronaldo"},
    {name: "Wayne Rooney"}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name
} AS output
Results
output

[ { "name": "Cristiano Ronaldo" } , { "name": "Lionel Messi" } , { "name": "Wayne Rooney" } ]