0

Groovy – @Sortable example

[last-modified]

Groovy @Sortable is something similar to Java‘s Comparable interface.

Using @Sortable

By default, SortablePerson objects in a list are sorted by ascending order using the order of properties declared in the class. For instance, objects are first ordered by lastName. If there are 2 objects that have the same lastName, sort them by firstName and so on.

The order of properties can be changed. For example, we want to order firstName and then lastName without actually reordering the properties in the class.

We can also exclude fields from the sorting process.

Sample Usage

Default sort

Outputs:

Sort by First Name, then Last Name

Outputs:

Sort by Last Name Only

OR

Outputs: