apoc.coll.sortMulti

Details

Syntax

apoc.coll.sortMulti(coll [, orderFields, limit, skip ])

Description

Sorts the given LIST<MAP<STRING, ANY>> by the given fields. To indicate that a field should be sorted according to ascending values, prefix it with a caret (^). It is also possible to add limits to the LIST<MAP<STRING, ANY>> and to skip values.

Arguments

Name

Type

Description

coll

LIST<MAP>

The list of maps to be sorted.

orderFields

LIST<STRING>

The property keys to be used to sort the list of maps by. The default is: [].

limit

INTEGER

The amount of results to return. The default is: -1.

skip

INTEGER

The amount to skip by. The default is: 0.

Returns

LIST<ANY>

Usage examples

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

apoc.coll.sortMulti
WITH [
  {name:'graphs'},
  {name:'are',age:32},
  {name:'everywhere',age:42}
] AS list
RETURN apoc.coll.sortMulti(list, ['^name']) as output
Using Cypher’s COLLECT subquery
WITH [
  {name:'graphs'},
  {name:'are',age:32},
  {name:'everywhere',age:42}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name
} AS output
Results
output

[{name: "are", age: 32}, {name: "everywhere", age: 42}, {name: "graphs"}]

The following examples sort a list of maps by the name property in ascending order and then by the age property in descending order using both APOC and Cypher:

apoc.coll.sortMulti
WITH [
  {name:'graphs'},
  {name:'are',age:32},
  {name:'are',age:21},
  {name:'everywhere'}
] AS list
RETURN apoc.coll.sortMulti(list, ['^name', 'age']) as output
Using Cypher’s COLLECT subquery
WITH [
  {name:'graphs'},
  {name:'are',age:32},
  {name:'are',age:21},
  {name:'everywhere'}
] AS list
RETURN COLLECT {
  UNWIND list AS x
  RETURN x ORDER BY x.name ASC, x.age DESC
} AS output
Results
Output

[{name: "are", age: 32}, {name: "are", age: 21}, {name: "everywhere"}, {name: "graphs"}]

The following examples sort a list of maps by the name property in ascending order and return only one value using both APOC and Cypher:

apoc.coll.sortMulti
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN apoc.coll.sortMulti(list, ['^name'], 1) as output
Using Cypher’s COLLECT subquery
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name LIMIT 1
} AS output
Results
Output

[{name: "are"}]

The following examples sort a list of maps by the name property in ascending order and skip the first value using both APOC and Cypher:

apoc.coll.sortMulti
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN apoc.coll.sortMulti(list, ['^name'], -1, 1) as output
Using Cypher’s COLLECT subquery
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name SKIP 1
} AS output
Results
Output

[{name: "everywhere"}, {name: "graphs"}]

The following examples sort a list of maps by the name property in ascending order, skip the first value, and return only one value using both APOC and Cypher:

apoc.coll.sortMulti
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN apoc.coll.sortMulti(list, ['^name'], 1, 1) as output
Using Cypher’s COLLECT subquery
WITH [
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
] AS list
RETURN COLLECT {
    UNWIND list AS x
    RETURN x ORDER BY x.name SKIP 1 LIMIT 1
} AS output
Results
Output

[{name: "everywhere"}]