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.
You can do this:
Expanded for explanation:
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: