I am sorting a list based on multiple fields.
sortedList.sort {[it.getAuthor(), it.getDate()]}
This works fine, but I want the date to be reversed and reverse() does not work.
How do I sort the author in ascending order but sort the date in descending (reverse) order?
Example of what I want:
Author Date
Adam 12/29/2011
Adam 12/20/2011
Adam 10/10/2011
Ben 11/14/2011
Curt 10/17/2010
Example of what I have:
Author Date
Adam 10/10/2011
Adam 12/20/2011
Adam 12/29/2011
Ben 11/14/2011
Curt 10/17/2010
For multi-property sorts like this you’ll get the most control if you use the
sort()with a closure or a Comparator, e.g.:Or a more concise version (courtesy of Ted Naleid):
I ran the above in groovysh on the following list:
And received the correctly sorted: