cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to return only keys from map based on have top 2 Values

I have a map like where keys are names, values are some count like e.g. {umakanth:10,sai:20,ravi:5,raju:25}

2X_d_d5e3503b951bb442b01f027d765032bc6f9913eb.png

I need only 2 keys in that map where corresponding Values are highest.
In this example I need to get 2 keys that is sai,raju.

Thanks in Advance .

2 REPLIES 2

webtic
Graph Fellow

Interesting question, there is no easy way to sort a map by value. APOC only supports it by key.
See http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/utilities/map-functions/ for more info.

What is your use-case? With a bit more context a different way of getting there might present itself.

You could do something like:

WITH {umakanth:10,sai:20,ravi:5,raju:25} as props
UNWIND keys(props) as key
WITH key, props[key] as value
ORDER BY value DESC
RETURN key, value
LIMIT 2