Mich Mich - 10 months ago 100
Groovy Question

Sorting ArrayList of Maps based on a map value

Let's say I have this

ArrayList
of
Map
s

def map1 = [key1:value1, key2:value2, key3:value3]
def map2 = [key1:value1, key2:value2, key3:value3]
def map3 = [key1:value1, key2:value2, key3:value3]
def maps = [map1, map2, map3]


I want to sort this list based on
value3
of the map.

How can I do that assuming that I'm working in Groovy?

Answer Source

You can create your own custom sort comparator like this in Groovy:

maps.sort { a, b -> a.key3 <=> b.key3 }

This will sort the maps based on the value of each one's key3.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download