Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:04:22+00:00 2026-05-28T15:04:22+00:00

How can I group map values by 2 different criteria to get the outputs

  • 0

How can I group map values by 2 different criteria to get the outputs 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'],
]
  1. 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   
    
  2. 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 the way to iterate to effectively loop through the group structure, and be able to construct the result (group / subgroup / name ).

Something like :

  1. for each group, print/output the group name;
  2. for each subgroup inside a group, print/output the subgroup name
  3. inside each subgroup, print the names.

It would yield the result outlined above.

As a nice aside, I would like to sort the whole data structure (groups and names).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:04:23+00:00Added an answer on May 28, 2026 at 3:04 pm

    For the first case:

    result = map.groupBy( { it.hobby }, { it.city } )
    

    and for the second:

    result = map.groupBy( { it.city }, { it.hobby } )
    

    You will end up with the original values in the map rather than just the name, but you will be able to do:

    result[ 'poker' ][ 'HK' ].name
    

    to get the result

    ["Ali", "Liu"]
    

    btw: This form of groupBy has only been available since Groovy 1.8.1, so if you’re stuck on an earlier version, this won’t work

    edit 2

    Based on your comment below, you can then do:

    result.each { a, b ->
      println "$a"
      b.each { c, d ->
        println "  $c"
        d.each {
          println "    $it.name"
        }
      }
    }
    

    This is the same logic as GVdP had in his answer, but I feel using the groupBy with multiple parameters like I have here makes your code more readable and obvious as to its intent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i group by type and then get every id of each member
How can I group nested collections based on column values, which are dynamically given
Trying to work with groupby so that I can group together files that were
I have the following SQLFiddle , i can group the results by time, count
How can I group a number of Dispatcher.Invoke and wait for one group to
Im about to develop notifications for my app. In whatsapp you can receive group
Can I Limit Group Items in the Content Query WebPart so that the output
How can I using group by with union in T-SQL? I want to group
I can post on group / page and user wall, but i don't want
I have a shape in Visio which is a group. The group shape can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.