tag:blogger.com,1999:blog-1280619439915049383.post1274695016933458505..comments2024-03-17T02:14:58.129-05:00Comments on James Lorenzen's Blog: Groovy Sort Listjlorenzenhttp://www.blogger.com/profile/13635369821860631868noreply@blogger.comBlogger39125tag:blogger.com,1999:blog-1280619439915049383.post-81998009907700138672018-02-15T09:20:14.233-06:002018-02-15T09:20:14.233-06:00you just saved me a bunch of extra work. thank you...you just saved me a bunch of extra work. thank youAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-51970133542862315972015-07-01T03:07:22.974-05:002015-07-01T03:07:22.974-05:00WOW, that was simple, thanks!WOW, that was simple, thanks!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-78526614018932121132013-04-15T13:58:51.694-05:002013-04-15T13:58:51.694-05:00Oops again. The documentation link I posted should...Oops again. The documentation link I posted should have been to the overloading of sort() that uses a closure: <br /><br />http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort(groovy.lang.Closure)Anonymoushttps://www.blogger.com/profile/05177388003939324161noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-51652952666870012102013-04-15T13:06:47.113-05:002013-04-15T13:06:47.113-05:00Oops! I now see that Anonymous beat me to that cas...Oops! I now see that Anonymous beat me to that case-insensitive solution by about five months -- and with more robust code too.Anonymoushttps://www.blogger.com/profile/05177388003939324161noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-2380651869049599672013-04-15T13:04:07.022-05:002013-04-15T13:04:07.022-05:00Five years later, James's post is still the be...Five years later, James's post is still the best thing on the web for Groovy newcomers, like me.<br /><br />After I used this to get my sorting done, I tried it again, but made the very mistake that the original post had warned about--using parentheses instead of curly braces (sorry, James, I need to read more slowly).<br /><br />Watch out for this mistake -- it produces mysterious error messages, such as "Cannot get property 'name' on null object" or "No such property: it for class".<br /><br />This experience prodded me into figuring out what the curly-brace syntax was actually doing<br /><br />(see http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort(java.util.Comparator) )<br /><br />which in turn suggests what I believe is the simplest solution to case-insensitive sorting (at least it worked for me): <br /><br />list.sort {it.name.toLowerCase()}Anonymoushttps://www.blogger.com/profile/05177388003939324161noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-17742129541951399552013-04-08T10:40:03.875-05:002013-04-08T10:40:03.875-05:00This continues to be very valuable information. Th...This continues to be very valuable information. Thanks for posting it.Anonymoushttps://www.blogger.com/profile/16560524830294132273noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-88827361439394171722012-11-27T19:29:40.167-06:002012-11-27T19:29:40.167-06:00If you want case insensitive searching but also pr...If you want case insensitive searching but also protection from nulls, try:<br /><br />.sort {it.name?.toLowerCase()}<br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-81228450059700514592012-03-17T09:52:56.232-05:002012-03-17T09:52:56.232-05:00Incredibly helpful. Thank you so much.Incredibly helpful. Thank you so much.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-34512158108145549712012-01-25T08:41:41.171-06:002012-01-25T08:41:41.171-06:00to Sonu
[[1,23],[1,13],[2,543],[2,573]].sort {a, ...to Sonu<br /><br />[[1,23],[1,13],[2,543],[2,573]].sort {a, b -> a[1].compareTo(b[1])}Юркhttps://www.blogger.com/profile/14114147997843245296noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-63122669247689844942011-12-01T05:47:33.211-06:002011-12-01T05:47:33.211-06:00HOw to sort this array value??
data = [[1,23],[1,1...HOw to sort this array value??<br />data = [[1,23],[1,13],[2,543],[2,573]]<br />I want to sort on the basis of second value of array like <br />[[1,13],[1,23],[2,543],[2,573]]<br /><br />ThanksSonuhttps://www.blogger.com/profile/16607702032336967553noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-8219216751432432982011-11-29T06:08:14.559-06:002011-11-29T06:08:14.559-06:00this blog even helped me...thanks a ton...this blog even helped me...thanks a ton...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-24004187923636262022011-10-21T00:28:08.012-05:002011-10-21T00:28:08.012-05:00Anon in Feb asked about sorting with multiple db f...Anon in Feb asked about sorting with multiple db fields.<br />I just did this, here's my solution. Elegance upgrades welcome!<br /><br />things.sort {thing1, thing2-> (thing1.field1<=>thing2.field1)?:(thing1.field2<=>thing2.field2)}<br /><br />Love the terminology ... "Elvis and the spaceships"<br /><br />Note, I actually wanted to reverse order the second field, so I negated the second sort:<br /><br />things.sort {thing1, thing2-> (thing1.field1<=>thing2.field1)?:-(thing1.field2<=>thing2.field2)}<br /><br />This is for two db fields ... multiple? does the structure hold up? for you to try if you want.Rob Mannoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-4896695231571680942011-07-27T15:37:31.911-05:002011-07-27T15:37:31.911-05:00Thank you very much. This really helped a lot.Thank you very much. This really helped a lot.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-91533428291083067232011-07-24T01:31:24.199-05:002011-07-24T01:31:24.199-05:00Amazing... I find more and more answers to my ques...Amazing... I find more and more answers to my questions on your blog. Big fan already. Thanks a bunch.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-73950596220891111962011-07-21T17:09:37.772-05:002011-07-21T17:09:37.772-05:00Thank you , it is still helping us.Thank you , it is still helping us.Mohananoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-81970192539003123882011-06-16T02:27:55.023-05:002011-06-16T02:27:55.023-05:00thanks alot!thanks alot!timonoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-85679939064142710562011-02-26T11:00:26.632-06:002011-02-26T11:00:26.632-06:00To sort case sensitive:
assert ["b",&qu...To sort case sensitive:<br /><br />assert ["b","A","C","d"].sort() == ["A","C","b","d"]<br /><br />To sort case insensitive:<br /><br />assert ["b","A","C","d"].sort{it.toLowerCase()} == ["A","b","C","d"]Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-90626106201725869262010-12-28T02:21:42.912-06:002010-12-28T02:21:42.912-06:00Is there any way to specify the order like in mysq...Is there any way to specify the order like in mysql we can specify "order by field(id, 2,3,1,4)". Can we specify such order on a property of domain class in grails criteria ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-15612836518696410342010-08-09T07:29:21.997-05:002010-08-09T07:29:21.997-05:00This post helped me greatly. Thanks. For reverse...This post helped me greatly. Thanks. For reverse order I found that you need to add .reverse() to ones sort closure.<br /><br />e.g. <br />myList.sort{ it.date}.reverse()Dem_Stillershttps://www.blogger.com/profile/09606032462562445541noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-14134250019185688992010-04-28T03:25:38.368-05:002010-04-28T03:25:38.368-05:00Fantastic, many thanks.
/MattiasFantastic, many thanks.<br /><br />/MattiasMattias Jonssonhttps://www.blogger.com/profile/02102604103013691161noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-63035401114375701962010-03-19T02:17:11.332-05:002010-03-19T02:17:11.332-05:00Great Job there. Can you give also show us how we ...Great Job there. Can you give also show us how we can apply this to our table listing on GSPs?retnyc.ubandohttps://www.blogger.com/profile/04271200115039022858noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-74115882347566343452010-03-19T02:15:57.670-05:002010-03-19T02:15:57.670-05:00Thanks for the great job.Can you give us an exmple...Thanks for the great job.Can you give us an exmple of how do we apply this on GSPs?retnyc.ubandohttps://www.blogger.com/profile/04271200115039022858noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-24496477148600027412009-12-15T14:58:20.844-06:002009-12-15T14:58:20.844-06:00To sort a list of map entries, use either:
list.s...To sort a list of map entries, use either:<br /><br />list.sort{it.key}<br /> or<br />list.sort{it.value}Rob Stoeckleinhttps://www.blogger.com/profile/04493751177709275100noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-64553985706109234402009-11-28T06:22:31.093-06:002009-11-28T06:22:31.093-06:00Nice, just what I was looking for. Thanks James.Nice, just what I was looking for. Thanks James.Ewanhttps://www.blogger.com/profile/13104318147706255144noreply@blogger.comtag:blogger.com,1999:blog-1280619439915049383.post-78072099939266648472009-10-09T07:09:18.780-05:002009-10-09T07:09:18.780-05:00To reverse sort order just add a dash before it. I...To reverse sort order just add a dash before it. I just tried this (but for numeric values) and it worked.<br /><br />list.sort{-it.name}<br /><br />//Henrik SjostrandHenrikhttps://www.blogger.com/profile/07084667392256024335noreply@blogger.com