I have a groovy list of Order object. I want to sort this list on order id. If my list is sorted in ascending order then sort it in descending and Vice Versa. What is the smarted way to solve this problem?
class Order {
int id
String toString() { "O$id" }
}
def list = [ new Order( id: 1 ), new Order( id: 2 ), new Order( id: 3 ) ]
// Sort ascending (modifies original list as well)
println list.sort { it.id }
// Sort descending (modifies original list as well)
println list.sort { -it.id }
// Sort ascending (don't modify original)
println list.sort( false ) { it.id }
// Sort descending (don't modify original)
println list.sort( false ) { -it.id }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With