apoc icon indicating copy to clipboard operation
apoc copied to clipboard

Inconsistency between apoc.coll.sort() and apoc.coll.sortNodes()

Open neo-technology-build-agent opened this issue 2 years ago • 0 comments

Issue by ThachNgocTran Thursday Jun 11, 2020 at 10:59 GMT Originally opened as https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1554


Expected Behavior (Mandatory)

The documentation doesn't say anything about sorting order or special character (ASCENDING_ORDER_CHAR) at the beginning of property name to indicate sorting order (in case of sortNodes()). So users assume that the sorting is ascending (from smallest to largest). And if users want descending, they can additionally use apoc.coll.reverse().

Actual Behavior (Mandatory)

By default, sort() does ascending sorting. In contrast, sortNodes() does descending sorting. I think we should make their behavior consistent. Beside, reading the source code of sortNodes() (link), I found that there is a hidden trick of using "^" (ASCENDING_ORDER_CHAR defined in the code) to specify which sorting order to carry out. But this trick is not disclosed in the documentation for sortNodes(). It is in apoc.coll.sortMulti(); however, this is another section.

Steps (Mandatory)

For apoc.coll.sort(), in Neo4j Browser:

RETURN apoc.coll.sort([1,3,2,4,6,5])
# Return ascendingly ordered list.

For apoc.coll.sortNodes(), in Neo4j Browser:

CREATE (a:User {age: 20}), (b:User {age: 50}), (c:User {age: 30})
# then:
MATCH (n:User) RETURN apoc.coll.sortNodes(COLLECT(n), "age")
# Return descendingly ordered list.

Specifications (Mandatory)

Currently used versions

Versions

  • OS: Windows 8.1 x64 (JDK 11.0.7 x64).
  • Neo4j: Neo4j Desktop v4.0.4
  • Neo4j-Apoc: v4.0.0.12