Monday, February 14, 2011

Reverse sort dropdown values in a GSP page

In a GSP page in a Grails application, it is easy to sort values in a dropdown in ascending order. Just pass a closure to sort() call with the name of the attribute to sort by.

  • ${i.name}

  • It is just as easy to sort in descending order. Just provide a sort closure with 2 parameters and return 1 or -1 as in a Comparator.compare() method.
    
    
  • ${i.name}
  • ... or see Tom's post below.

    1 comment:

    1. Yeah, that's nice. :)

      Here's another alternative for sorting in descending order in this example - by delegating to String.compareTo() of the name attribute:

      coll.sort{a,b -> b.name<=>a.name}

      ReplyDelete