Skip to content

Instantly share code, notes, and snippets.

@jonatasemidio
Created August 10, 2012 17:01
Show Gist options
  • Save jonatasemidio/3315640 to your computer and use it in GitHub Desktop.
Save jonatasemidio/3315640 to your computer and use it in GitHub Desktop.
Ordenação de lista em groovy
List list = [
[id:0, firstName: 'Sachin', lastName: 'Tendulkar', age: 40 ],
[id:1, firstName: 'Sachin', lastName: 'Tendulkar', age: 103 ],
[id:2, firstName: 'Ajay', lastName: 'Tendulkar', age: 48 ],
[id:3, firstName: 'Virendra', lastName: 'Sehwag', age: 5 ],
[id:4, firstName: 'Virendra', lastName: 'Sehwag', age: 50 ],
[id:5, firstName: 'Sachin', lastName: 'Nayyar', age: 15 ]
]
println list.sort { a,b -> a.firstName <=> b.firstName ?: a.lastName <=> b.lastName ?: a.age <=> b.age}*.id
println list.sort{it.firstName+it.lastName+String.format('%3d', it.age)}*.id
/*
fonte: http://www.intelligrape.com/blog/2012/08/10/groovier-way-of-sorting-over-multiple-fields-in-a-list-of-maps-in-groovy/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment