How can I group map values according to two different criteria to get the results below?
def listOfMaps = [ [name:'Clark', city:'London', hobby: 'chess'], [name:'Sharma', city:'London', hobby: 'design'], [name:'Maradona', city:'LA', hobby: 'poker'], [name:'Zhang', city:'HK', hobby: 'chess'], [name:'Ali', city: 'HK', hobby: 'poker'], [name:'Liu', city:'HK', hobby: 'poker'], [name:'Doe', city:'LA', hobby: 'design'], [name:'Smith', city:'London', hobby: 'poker'], [name:'Johnson', city: 'London', hobby: 'poker'], [name:'Waters', city:'LA', hobby: 'poker'], [name:'Hammond', city:'LA', hobby: 'design'], [name:'Rogers', city:'LA', hobby: 'chess'], ]
Group order: hobby, city
poker London Smith Johnson LA Maradona Waters HK Ali Liu design London Sharma LA Doe Hammond HK chess London Clark LA Rogers HK Zhang
Group order: city, hobby
London poker Smith Johnson design Sharma chess Clark LA poker Maradona Waters design Doe Hammond chess Rogers HK poker Ali Liu design chess Zhang
Edit:
What I really need is an iteration method to efficiently loop through the group structure and the ability to build the result (group / subgroup / name).
Sort of:
- for each group, print / print the name of the group;
- for each subgroup within the group, type / print the name of the subgroup
- inside each subgroup, type the names.
This will give the result described above.
As well, I would like to sort the entire data structure (groups and names).