11

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)
Alexei Tenitski
  • 9,030
  • 6
  • 41
  • 50

2 Answers2

15

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]
Alexei Tenitski
  • 9,030
  • 6
  • 41
  • 50
0

I had similar issue but with except(sth).unique() and limit [0..5] . In my case:

ERROR:

except(xxx).unique()[0..5]

Works FINE:

except(sth).unique().findAll()[0..5]

Works FINE also with sort{}:

.unique().findAll().sort{it.sth}[0..5]

(Alexei Tenitski answer is good too)

fider
  • 1,976
  • 26
  • 29