[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.turreta.groovy import groovy.transform.Sortable import groovy.transform.ToString @ToString @Sortable class SortablePerson { String lastName String firstName } |
[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.turreta.groovy import groovy.transform.Sortable import groovy.transform.ToString @ToString @Sortable(includes = ['firstName', 'lastName']) class SortablePerson { String lastName String firstName } |
We can also exclude fields from the sorting process.
1 | @Sortable(excludes = ['lastName']) |
Sample Usage
1 2 3 4 5 6 7 8 9 10 11 12 | def sp1 = new SortablePerson(lastName: "Smith", firstName: "Angie") def sp2 = new SortablePerson(lastName: "Smith", firstName: "Bernie") def sp3 = new SortablePerson(lastName: "Scott", firstName: "Karl") def sp4 = new SortablePerson(lastName: "Woods", firstName: "Tiger") def sp5 = new SortablePerson(lastName: "Jackson", firstName: "Micheal") def sp6 = new SortablePerson(lastName: "Taylor", firstName: "Maui") def sp7 = new SortablePerson(lastName: "Jackson", firstName: "Mark") def sp8 = new SortablePerson(lastName: "Taylor", firstName: "Bonnie") def list = [sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8] println list.toSorted() |
[wp_ad_camp_3]
Default sort
1 2 3 4 5 6 | ... @ToString @Sortable class SortablePerson { ... } |
Outputs:
1 2 3 4 5 6 7 8 | [com.turreta.groovy.SortablePerson(Jackson, Mark), com.turreta.groovy.SortablePerson(Jackson, Micheal), com.turreta.groovy.SortablePerson(Scott, Karl), com.turreta.groovy.SortablePerson(Smith, Angie), com.turreta.groovy.SortablePerson(Smith, Bernie), com.turreta.groovy.SortablePerson(Taylor, Bonnie), com.turreta.groovy.SortablePerson(Taylor, Maui), com.turreta.groovy.SortablePerson(Woods, Tiger)] |
Sort by First Name, then Last Name
1 2 3 4 5 | @ToString @Sortable(includes = ['firstName', 'lastName']) class SortablePerson { ... } |
Outputs:
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 | [com.turreta.groovy.SortablePerson(Smith, Angie), com.turreta.groovy.SortablePerson(Smith, Bernie), com.turreta.groovy.SortablePerson(Taylor, Bonnie), com.turreta.groovy.SortablePerson(Scott, Karl), com.turreta.groovy.SortablePerson(Jackson, Mark), com.turreta.groovy.SortablePerson(Taylor, Maui), com.turreta.groovy.SortablePerson(Jackson, Micheal), com.turreta.groovy.SortablePerson(Woods, Tiger)] |
Sort by Last Name Only
1 2 3 4 5 | @ToString @Sortable(includes = ['lastName']) class SortablePerson { ... } |
OR
1 2 3 4 5 | @ToString @Sortable(excludes = ['firstName']) class SortablePerson { ... } |
Outputs:
1 2 3 4 5 6 7 8 | [com.turreta.groovy.SortablePerson(Jackson, Micheal), com.turreta.groovy.SortablePerson(Jackson, Mark), com.turreta.groovy.SortablePerson(Scott, Karl), com.turreta.groovy.SortablePerson(Smith, Angie), com.turreta.groovy.SortablePerson(Smith, Bernie), com.turreta.groovy.SortablePerson(Taylor, Maui), com.turreta.groovy.SortablePerson(Taylor, Bonnie), com.turreta.groovy.SortablePerson(Woods, Tiger)] |
[wp_ad_camp_1]