下面的simplegraph-core测试套件中的Unit测试代码显示了机场的区域数量,但是没有按我期望的那样进行排序。

结果开始于:

NZ-BOP=  3
MZ-A=  1
MZ-B=  1
IN-TN=  5
MZ-N=  1
PW-004=  1
MZ-I=  2
BS-FP=  1
IN-TR=  1
MZ-T=  1
BJ-AQ=  1
GB-ENG= 27

我看着
  • Gremlin group by vertex property and get sum other properties in the same vertex
  • Gremlin query for groupcount of last value

  • 并在标记为gremlin的问题中搜索“GroupCount”无济于事

    需要什么来解决订购问题?

    单元测试
    另请参阅https://github.com/BITPlan/com.bitplan.simplegraph/blob/master/simplegraph-core/src/test/java/com/bitplan/simplegraph/core/TestTinkerPop3.java
      @Test
      public void testSortedGroupCount() throws Exception {
        Graph graph = getAirRoutes();
        GraphTraversalSource g = graph.traversal();
        Map<Object, Long> counts = g.V().hasLabel("airport").groupCount()
            .by("region").order().by(Order.decr).next();
        assertEquals(1473, counts.size());
        for (Object key : counts.keySet()) {
          System.out.println(String.format("%s=%3d", key, counts.get(key)));
        }
      }
    

    最佳答案

    您需要使用values作用域来订购local:

    g.V().hasLabel("airport").
      groupCount().
        by("region").
      order(local).
        by(values, Order.decr)
    

    local范围内,您可以在当前遍历器中进行排序(即在遍历中对每个Map的内容进行排序)。
      @Test
      public void testSortedGroupCount() throws Exception {
        Graph graph = getAirRoutes();
        GraphTraversalSource g = graph.traversal();
        Map<Object, Long> counts = g.V().hasLabel("airport").groupCount()
            .by("region").order(Scope.local).by(Column.values,Order.decr).next();
        // https://stackoverflow.com/a/49361250/1497139
        assertEquals(1473, counts.size());
        assertEquals("LinkedHashMap",counts.getClass().getSimpleName());
        debug=true;
        if (debug)
          for (Object key : counts.keySet()) {
            System.out.println(String.format("%s=%3d", key, counts.get(key)));
          }
    
      }
    

    然后将显示:
    US-AK=149
    AU-QLD= 50
    CA-ON= 46
    CA-QC= 44
    PF-U-A= 30
    US-CA= 29
    

    10-06 09:06