How do you order the result of a groupCount by value DESC and then Key ASC?

1,489 views
Skip to first unread message

Tomer Sagi

unread,
Oct 28, 2014, 9:26:14 AM10/28/14
to gremli...@googlegroups.com
Hi,
If I do this: ....groupCount(qRes,{it.name}).cap().orderMap(T.decr)[0..<10]
I get the map ordered by the count values. Since I have multiple rows for each value, I would like to add a secondary ordering by the group key. 
In SQL, that would amount to 

SELECT Key, Value
FROM SomeBigInnerQuery
ORDER BY Key DESC, Value ASC

How would you accomplish the same in Gremlin?
Thanks
Tomer

Daniel Kuppitz

unread,
Oct 28, 2014, 11:45:05 AM10/28/14
to gremli...@googlegroups.com
Your SQL query doesn't do any grouping. It would be more like this. With groupCount involved it gets trickier, maybe try this:

...groupCount().cap().next().sort { a, b -> b.value <=> a.value ?: a.key.anotherProp <=> b.key.anotherProp }.collect { it.key }[0..<10]

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/5f14a29d-c7f8-42d7-8438-983158736561%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomer Sagi

unread,
Oct 29, 2014, 3:57:38 AM10/29/14
to gremli...@googlegroups.com
You've lost me here. sort is not a gremlin step, is it groovy?
It's just that I'm translating your suggestions to JVM and there I get an Object after Next. 
So, what you suggest is to iterate the result after the groupCount and then to sort externally to the gremlin pipeline. 
I could just use Collections.sort in Java. right?

Daniel Kuppitz

unread,
Oct 29, 2014, 7:51:00 AM10/29/14
to gremli...@googlegroups.com
It's just that I'm translating your suggestions to JVM and there I get an Object after Next. 

Yes, the Object should be a Map.

So, what you suggest is to iterate the result after the groupCount and then to sort externally to the gremlin pipeline. 

Correct. You could also use Gremlin to iterate over the Map entries, but that's an unnecessary overhead IMO.

I could just use Collections.sort in Java. right?

Whatever you want. I prefer the nifty Groovy syntax for this sorting example.

Cheers,
Daniel



Steven Tang

unread,
Nov 16, 2015, 6:33:47 PM11/16/15
to Gremlin-users
Is there an equivalent for this using just the JVM? starting with a GremlinPipeline?

For example:

GremlinPipeline pipe = new GremlinPipeline<Vertex, Vertex>();

pipe.start(someVertex)
.out()
.out() // a pipe with duplicate vertices
.groupCount()
.cap()
.order(Some Pipe Function)
.map()
.toList()

The hope is the get a sorted list based on the group counts using just using GremlinPipeline functions.

Daniel Kuppitz

unread,
Nov 16, 2015, 7:01:23 PM11/16/15
to gremli...@googlegroups.com
So you need the counts only to order your result set? In that case simply do this:

pipe.start(someVertex).out().out().groupCount().cap().
     orderMap(T.decr).map().toList()

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages