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))

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))
2 Likes