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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:26:49+00:00 2026-06-06T07:26:49+00:00

I wanted to implement a default sort order in my domain class and immediately

  • 0

I wanted to implement a default sort order in my domain class and immediately found it didn’t work with the getAll method. No biggie, I just used list instead. The thing is that the default sort order in a domain class does not allow you specify multiple sort fields (as seen here).

My goal is to sort all Foo objects first by the name of their Bar object, then by their own name.

class Foo {
    String name
    String Bar
}

class Bar {
    String name
}

How can I implement this in the domain class so I don’t have to specify a long/nasty comparator every time I call .list()?

One of my attempts:

static Comparator getComparator() { 
    def c = { a, b -> 
    def result = a.bar.name.compareTo( b.bar.name ); 
        if ( result == 0 ) {
            result = a.name.compareTo( b.name );
        }
    }
    return c as Comparator
}

Then I could just call Foo.list(Foo.getComparator())… if I could get it to work.

Update:

I think I am really close here, just having trouble with implementing two comparisons in the same sort closure.

Foo.list().sort{ a, b ->
    def result = a.bar.name <=> b.bar.name;
    // Things mess up when I put this if statement in.
    if( result == 0 ) {
        a.name <=> b.name
    }
}

Disco!

class Foo { // My domain class
    // ...

    static Comparator getComparator() {
        def c =[
            compare: { a, b ->
                def result = a.bar.name <=> b.bar.name;
                if( result == 0 ) {
                    result = a.name <=> b.name
                }
                return result
            }
        ] as Comparator
    }

    // ...
}

And implemented like this in my controller:

Foo.list().sort( Foo.getComparator() )

PS:

The above works, but Jeff Storey posted some code in his answer after I discoed, and his code works and is much nicer than mine so use it 🙂

  • 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-06T07:26:51+00:00Added an answer on June 6, 2026 at 7:26 am

    In your case, would it make sense to have Foo implement Comparable and the implementation could do the comparison as you described? Then when you sort the objects in a list, because they are Comparable, they will sort properly.

    If it does not make sense for you to implement Comparable though, you will need to specify a comparator to sort by.

    Here’s some sample code based on your comments:

    edit:

    class Person implements Comparable<Person> {
    
       String firstName
       String lastName
    
       int compareTo(Person other) {
           int lastNameCompare = lastName <=> other.lastName
           return lastNameCompare != 0 ? lastNameCompare : firstName <=> other.firstName
       }
    
       String toString() {
         "${lastName},${firstName}"
       }
    }
    
    def people = [new Person(firstName:"John",lastName:"Smith"), new Person(firstName:"Bill",lastName:"Jones"), new Person(firstName:"Adam",lastName:"Smith")]
    println "unsorted = ${people}"
    println "sorted = ${people.sort()}" 
    

    This prints:

    unsorted = [Smith,John, Jones,Bill, Smith,Adam]
    sorted = [Jones,Bill, Smith,Adam, Smith,John]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to implement a method in a abstract class that is called by
I'm kind of new to algorithms and wanted to implement heap sort algorithm. The
I wanted to implement a Node type like written below but I want to
I wanted to implement a search feature for my web app. I choose Haystack
I wanted to implement python com server using win32com extensions. Then consume the server
I wanted to implement the game Pacman. For the AI, I was thinking of
I wanted to implement a simple python program using parallel execution. It's I/O bound,
I always wanted to implement swypable tabs in my application, like the ones in
If I wanted to implement an app for 2 Android devices so I can
Let's say you wanted to implement a breadth-first search of a binary tree recursively

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.