What is the ordering of group?

For instance (on Tinkerpop modern)
g.V().group().by('age').by('name')
gives
[{32=[josh], 35=[peter], 27=[vadas], 29=[marko]}]

There must be some logic to it?

(Understanding this would help me for unit testing, allowing me to compare results that are actually the same)
Solution
Gremlin doesn't offer too many order guarantees. I think group() by default uses a regular old HashMap for it's data structure. If you want an order you would want to specifically specify an order()
  • for example:
    gremlin> g.V().group().by('age').by('name').order(local).by(keys)
    ==>[27:[vadas],29:[marko],32:[josh],35:[peter]]

    I suppose you could also force use of a LinkedHashMap:
    gremlin> g.withSideEffect('m', [:]).V().hasLabel('person').group('m').by('age').by('name').cap('m')
    ==>[29:[marko],27:[vadas],32:[josh],35:[peter]]
    gremlin> g.withSideEffect('m', [:]).V().order().by('age').group('m').by('age').by('name').cap('m')
    ==>[27:[vadas],29:[marko],32:[josh],35:[peter]]

    The first being an insertion order given however the underlying graph iterates vertices from V() or with an explicit order() as in the second. I'd say it's possible better to do the explicit order against vertices rather than than my first example where it does a in-memory sort of the Map as it's possible you might get some performance improvement there depending on your graph. Hard to say which is nicer as the withSideEffect() approach isn't quite as readable. (Note that in my Groovy examples above, the [:] will resolve to a LinkedHashMap by default.)
Was this page helpful?