Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting ArrayList of Maps based on a map value

Let's say I have this ArrayList of Maps

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?

like image 697
Mich Avatar asked Dec 11 '22 21:12

Mich


1 Answers

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.

like image 115
thecodesmith_ Avatar answered Jan 01 '23 06:01

thecodesmith_