How to sort AND limit Noe4j result using Gremlin?

graph, gremlin, neo4j, sorting

Solution

It took me a while to figure out that native Groovy methods like sort do not return Pipes, but iterators, iterables, etc. As such, to convert one of these objects back into a Pipeline flow you need to use _():

g.v(id).out('knows').sort{it.name}._()[0..9]

Problem

This is how you can sort (order) results from Neo4j graph using Gremlin: ``` g.v(id).out('knows').sort{it.name} ``` or ``` g.v(id).out('knows').sort{a,b -> a.name <=> b.name} ``` This is how to limit result using offset/skip and limit: ``` g.v(id).out('knows')[0..9] ``` However if you combine both sort and limit ``` g.v(id).out('knows').sort{it.name}[0..9] ``` it would throw an error... ``` javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList$ListItr.getAt() is applicable for argument types: (groovy.lang.IntRange) values: [0..9] Possible solutions: getAt(java.lang.String), getAt(int), next(), mean(), set(java.lang.Object), putAt(java.lang.String, java.lang.Object) ```

Original source