3
votes

This is a newbie question, any help will be appreciated. I have a class Item as follows.

class Item {
    String name
    // other properties ...

    static constraints = {
        name(blank: false, unique: true)
        // other constraints ...
    }
}

How do I persist the Items in a sorted order in Grails/GORM? Meaning, if I did

new Item(name: 'a').save(flush: true, failOnError: true)
new Item(name: 'c').save(flush: true, failOnError: true)
new Item(name: 'b').save(flush: true, failOnError: true)

println Item.getAll().name

I should get [a, b, c]. Thank you!

2

2 Answers

1
votes

Add this in your domain class

static mapping = {
    sort name: "desc" //"asc"
}
4
votes

Presisting doesn't matter but ,what matters the way you retrieve it and you can simply do as follow

println Item.list().sort() 

println Item.list().sort(){ //write your groovy clouser here to sort asc and dec }