Software Development

Groovy – @Sortable example

[last-modified]

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

Using @Sortable

[wp_ad_camp_5]

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.

[wp_ad_camp_4]

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

[wp_ad_camp_3]

Default sort

Outputs:

Sort by First Name, then Last Name

Outputs:

[wp_ad_camp_2]

Sort by Last Name Only

OR

Outputs:

[wp_ad_camp_1]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like