apoc.coll.sortMulti

Function APOC Core

apoc.coll.sortMulti(coll, ['^name','age'],[limit],[skip]) - sort list of maps by several sort fields (ascending with ^ prefix) and optionally applies limit and skip

Signature

apoc.coll.sortMulti(coll :: LIST? OF MAP?, orderFields = [] :: LIST? OF STRING?, limit = -1 :: INTEGER?, skip = 0 :: INTEGER?) :: (LIST? OF ANY?)

Input parameters

Name Type Default

coll

LIST? OF MAP?

null

orderFields

LIST? OF STRING?

[]

limit

INTEGER?

-1

skip

INTEGER?

0

Usage Examples

The following sorts a list of maps by the name property in ascending order:

RETURN apoc.coll.sortMulti([
  {name:'graphs'},
  {name:'are',age:32},
  {name:'everywhere',age:42}
], ['^name']) as output;
Table 1. Results
Output

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

The following sorts a list of maps by the name property in ascending order and then the age property in descending order:

RETURN apoc.coll.sortMulti([
  {name:'graphs'},
  {name:'are',age:32},
  {name:'are',age:21},
  {name:'everywhere'}
], ['^name', 'age']) as output;
Table 2. Results
Output

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

The following sorts a list of maps by the name property in ascending order and returns only one value:

RETURN apoc.coll.sortMulti([
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
], ['^name'], 1) as output;
Table 3. Results
Output

[{name: "are"}]

The following sorts a list of maps by the name property in ascending order and skips the first value:

RETURN apoc.coll.sortMulti([
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
], ['^name'], -1, 1) as output;
Table 4. Results
Output

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

The following sorts a list of maps by the name property in ascending order, skips the first value, and returns only one value:

RETURN apoc.coll.sortMulti([
  {name:'graphs'},
  {name:'are'},
  {name:'everywhere'}
], ['^name'], 1, 1) as output;
Table 5. Results
Output

[{name: "everywhere"}]