【问题标题】:Neo4j: ordering output by values in a collectionNeo4j:按集合中的值排序输出
【发布时间】:2020-11-05 04:31:01
【问题描述】:

我正在处理包含四列数据的 csv 文件中的数据:stationIDstationNamestationLongstationLat 其中stationID 是电台的唯一编号,stationName 是电台名称,stationLong是电台所在位置的经度,stationLat是电台所在位置的纬度。

样本数据如下:

第 1 行 1, "Cranbourne", 22.6932, 100.5347

第 2 行 2, "Dandenong", 25.6589, 98.5679

第 3 行 3, "Flinders Street", 33.1674, 66.3287

此数据已加载到 Neo4J 并标记为 Station

我已运行查询以获取从每个站点到所有其他站点的距离。例如,“Cranbourne”站到“Dandenong”、“Cranbourne”到“Flinders Street”的距离等等。

MATCH (s1: Station), (s2: Station)
WHERE s1 <> s2
RETURN s1.stationName AS `Station`, collect([s2.stationName, round(distance(
    point({longitude: s1.stationLong, latitude: s1.stationLat}),
    point({longitude: s2.stationLong, latitude: s2.stationLat})))]) AS `Distance to all other stations`

输出:

第一行:"Cranbourne", [["Dandenong", 15], ["Flinders Street", 45], ["Springvale", 25], ["Yarraman", 19]]

这个输出是我想要的结果,但是有没有办法确保输出按与车站的距离升序排列?

期望的输出:

第一行:"Cranbourne", [["Dandenong", 15], ["Yarraman", 19], ["Springvale", 25], ["Flinders Street", 45]]

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您可以在COLLECT它之前对数据进行排序:

    MATCH (s1: Station), (s2: Station)
    WHERE s1 <> s2
    WITH
      s1.stationName AS Station, s2.stationName AS sn2,
      round(distance(
        point({longitude: s1.stationLong, latitude: s1.stationLat}),
        point({longitude: s2.stationLong, latitude: s2.stationLat}))) AS dist
    ORDER BY dist
    RETURN Station, COLLECT([sn2, dist]) AS `Distance to all other stations`
    

    【讨论】:

      【解决方案2】:

      你有两个选择:

      • 在执行 COLLECT 之前获取您单独排序的值。
      • 收集后使用 apoc.coll.sort 或 apoc.coll.sortMaps 进行排序

      【讨论】:

        猜你喜欢
        • 2022-11-23
        • 2021-09-20
        • 2014-08-30
        • 2019-05-26
        • 2015-10-18
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多