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 9305957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:03:24+00:00 2026-06-19T00:03:24+00:00

Trying to sort a map based on one field in a controller before it

  • 0

Trying to sort a map based on one field in a controller before it is passed to the gsp, and it looks to be working fine in the controller, however the gsp page seems to randomly grab the items in no specific order.

Controller code. Trying to order the prompts that will be displayed.

 def show(Long id) {
    def reportInstance = Report.get(id)
    reportInstance.prompts = reportInstance.prompts.sort{it.displaySequence}
    [reportInstance: reportInstance]
}

If I put that in a println statement, it shows it sorted in the console.

Domain object that it is working with:

class Report {

    Long id
    String name
    String department
    String description
    String summary
    Date activityDate

    static hasMany = [prompts:Prompt]

    static mapping = {
            sort "id"
           version false
           table 'reports'
           columns{
                   id column: 'id'
                   name column: 'name'
                   department column: 'department'
                   description column: 'description'
                   summary column: 'summary'
                   activityDate column: 'activity_date'
           }
           id generator:'sequence', params:[sequence:'reports_id_sequence']
    }

    static constraints = {
           name(nullable: false, maxSize: 60)
           department(nullable: false, maxSize: 60)
           description(nullable: true, maxSize: 120)
           summary(nullable: true, maxSize: 500)
           activityDate(nullable: false)

    }

    String toString() {
        "${name}"
    }

Here is the snippet from the gsp page in question.

<g:if test="${reportInstance?.prompts}">
    <li class="fieldcontain">
        <h3 class="property-label">Prompts</h3>
        <br>
        <table id="prompts">
            <thead>
                <tr>
                    <th>${message(code: 'prompt.name.label', default: 'Name')}</th>
                    <th>${message(code: 'prompt.description.label', default: 'Description')}</th>
                    <th>${message(code: 'prompt.required.label', default: 'Required')}</th>
                    <th>${message(code: 'prompt.displaySeqno.label', default: 'Display Order')}</th>
                </tr>
            </thead>
            <tbody>
                <g:each in="${reportInstance.prompts}" status="i" var="prompt">
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}" onclick='window.location = "${createLink(controller: "prompt", action: "edit", id: prompt.id)}"'>
                        <td>${fieldValue(bean: prompt, field: "name")}</td>
                        <td>${fieldValue(bean: prompt, field: "description")}</td>
                        <td>${prompt.required}</td>
                        <td class="displaySeqno">${prompt.displaySeqno}</td>
                    </tr>
                </g:each>
            </tbody>
        </table>
    </li>
</g:if>
  • 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-06-19T00:03:26+00:00Added an answer on June 19, 2026 at 12:03 am

    In your Report domain class prompts is a Set, not a List, so you can’t sort it like that. You’ll need to pass the sorted list separately in the model:

    def show(Long id) {
        def reportInstance = Report.get(id)
        [reportInstance: reportInstance,
         prompts:reportInstance.prompts.sort{it.displaySequence}]
    }
    

    and use that in the GSP

    <g:each in="${prompts}" status="i" var="prompt">
    

    or just pass the reportInstance and do the sorting in the GSP

    <g:each in="${reportInstance.prompts.sort{it.displaySequence}}"
        status="i" var="prompt">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to sort out my tile map for my 2d tile based game
I trying to sort this HashMap based on date in keys My Hash map:
I'm trying to make a sort of map with data that overlays based on
Been trying to sort this one out for a while. I'd really appreciate any
I'm working on Ruby on rails 2.3.4 and I'm trying to develop a Sort
Trying to place a header bar at top of a full page Google Map
I'm trying to sort a map in Groovy that has maps as value. I
I am trying to sort the below map( Clojure) on the basis of col_nm
I'm trying to dynamically populate a map based off a list of objects in
Hi am trying to sort a map using the map's key finalMap.entrySet().sort{it.key} But the

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.