cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Collection Sort

Hi, how to sort the id(n) inside the collect based on the distances by ascending order.
the below query doesn't sort the distance.

MATCH (n:PointNodes)  
WITH n, distance(point({longitude:n.lon,latitude:n.lat}), point({ longitude: -1.7444, latitude: 23.7971 })) as dist
where dist < 200
RETURN collect(id(n))
1 ACCEPTED SOLUTION

Hello,

No sorting is taking place because you haven't specified any kind of ordering. You need to sort before the collect, which will ensure the resulting list is ordered:

MATCH (n:PointNodes)  
WITH n, distance(point({longitude:n.lon,latitude:n.lat}), point({ longitude: -1.7444, latitude: 23.7971 })) as dist
WHERE dist < 200
WITH n, dist
ORDER BY dist ASC
RETURN collect(id(n))

View solution in original post

1 REPLY 1

Hello,

No sorting is taking place because you haven't specified any kind of ordering. You need to sort before the collect, which will ensure the resulting list is ordered:

MATCH (n:PointNodes)  
WITH n, distance(point({longitude:n.lon,latitude:n.lat}), point({ longitude: -1.7444, latitude: 23.7971 })) as dist
WHERE dist < 200
WITH n, dist
ORDER BY dist ASC
RETURN collect(id(n))