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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:34:14+00:00 2026-06-14T05:34:14+00:00

Hello I am using Grails 2.1 and I have a little problem I can’t

  • 0

Hello I am using Grails 2.1 and I have a little problem I can’t find a good solution to. I am querying the database for some objects and it returns and sort it as expected with one exception, the null value is first.

Here is the code I may use to query the database:

Object.findAllByObjectTypeAndDateBetween(ObjectType.REGULAR, startDate, stopDate).sort {it.str}

Is there any way I can sort by the strings and getting all null values last instead of first? I am looking for a simple way, not like this:
Grails/Hibernate: how to order by isnull(property) to get NULLs last?

Thanks.

  • 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-14T05:34:15+00:00Added an answer on June 14, 2026 at 5:34 am

    You can do this:

    objects.sort { a, b ->
      !a.str ? !b.str ? 0 : 1 : !b.str ? -1 : a.str <=> b.str
    }
    

    Expanded for explanation:

    objects.sort { a, b ->
      if( !a.str ) {             // If a.str is null or empty
        if( !b.str ) {           // If b.str is null or empty 
          return 0               // They are the same
        }
        else {                   // a.str is empty or null, but b.str has value
          return 1               // b.str should come before a.str
        }
      else {                     // a.str has value
        if( !b.str ) {           // b.str is null or empty
          return -1              // b.str should come after a.str
        }
        else {                   // Both have value, 
          return a.str <=> b.str // Compare them 
        }
      }
    

    This will put null strings and empty strings at the end of the list, and sort the rest alphabetically

    If you want the empty strings at the head of the list (and the nulls at the tail), you’d need to explicitly check for null rather than relying on Groovy Truth:

    objects.sort { a, b ->
      a.str == null ? b.str == null ? 0 : 1 : b.str == null ? -1 : a.str <=> b.str
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm using flexigrid and i have a problem, i don't know why it
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Hello i am using SocializeActionBar for my app there i have multiple links. I
Hello i am using NSOperationQueue to download images in the background. I have created
hello i am using the nimbus look-and-feel and have a tabbedpane with an icon
Hello i am using following xml to create some sort of Transparent panel .
I have recently tried to start using Grails and now I want to integrate
I have two files: hello.h and hello.cpp hello.h #ifndef __HELLO_H__ #define __HELLO_H__ using namespace
Hello i am using ORMLite 4.33. I have an entity class that gives me
I'm encountering a strange problem with the session using GXT 2.1 and a Grails

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.