7.2.2.3 Search and Filtering 3.0.10

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 7.2.2.3 Search and Filtering - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.0.10 ## 7.2.2.3 Search and Filtering If you have collections of objects you often need to sort and filter them. Use the findAll and grep tags for these tasks:
    Stephen King's Books:
    <g:findAll in="${books}" expr="it.author == 'Stephen King'">
         <p>Title: ${it.title}</p>
    </g:findAll>
The `expr` attribute contains a Groovy expression that can be used as a filter. The grep tag does a similar job, for example filtering by class:
    <g:grep in="${books}" filter="NonFictionBooks.class">
         <p>Title: ${it.title}</p>
    </g:grep>
Or using a regular expression:
    <g:grep in="${books.title}" filter="~/.*?Groovy.*?/">
         <p>Title: ${it}</p>
    </g:grep>
The above example is also interesting due to its usage of GPath. GPath is an XPath-like language in Groovy. The `books` variable is a collection of `Book` instances. Since each `Book` has a `title`, you can obtain a list of Book titles using the expression `books.title`. Groovy will auto-magically iterate the collection, obtain each title, and return a new list! | Quick Reference (hide)