I’m trying to sort a map in Groovy that has maps as value. I want to iterate over the map and print out the values sorted by lastName and firstName values. So in the following example:
def m =
[1:[firstName:'John', lastName:'Smith', email:'john@john.com'],
2:[firstName:'Amy', lastName:'Madigan', email:'amy@amy.com'],
3:[firstName:'Lucy', lastName:'B', email:'lucy@lucy.com'],
4:[firstName:'Ella', lastName:'B', email:'ella@ella.com'],
5:[firstName:'Pete', lastName:'Dog', email:'pete@dog.com']]
the desired results would be:
[firstName:'Ella', lastName:'B', email:'ella@ella.com']
[firstName:'Lucy', lastName:'B', email:'lucy@lucy.com']
[firstName:'Pete', lastName:'Dog', email:'pete@dog.com']
[firstName:'Amy', lastName:'Madigan', email:'amy@amy.com']
[firstName:'John', lastName:'Smith', email:'john@john.com']
I’ve tried m.sort{it.value.lastName&&it.value.firstName} and m.sort{[it.value.lastName, it.value.firstName]}. Sorting by m.sort{it.value.lastName} works but does not sort by firstName.
Can anybody help with this, much appreciated, thanks!
This should do it: