{"id":2699,"date":"2011-01-25T00:59:03","date_gmt":"2011-01-24T19:29:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2699"},"modified":"2016-12-15T18:25:00","modified_gmt":"2016-12-15T12:55:00","slug":"groovy-sort-list-of-objects-on-the-basis-of-more-than-one-field","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-sort-list-of-objects-on-the-basis-of-more-than-one-field\/","title":{"rendered":"Groovy: Sort list of objects on the basis of more than one field"},"content":{"rendered":"<p>Usually, when we deal with databases, we don&#8217;t face such kind of situation because we query database to get result-set in required order.<\/p>\n<p>But let&#8217;s say you have a List of objects that you want to sort on the basis of two fields. Let&#8217;s discuss it with an example.<\/p>\n<p>I have a list of Tasks (with date and priority fields). I want to sort these tasks ordered by Date and priority-wise (as second level sorting).<\/p>\n<p>Here, I am going to use Expando class, so I can directly run this in my groovyConsole. But definitely, you can use some &#8216;Task&#8217; class.<\/p>\n<p>[groovy]<br \/>\nExpando a = new Expando(date: Date.parse(&#8216;yyyy-MM-dd&#8217;,&#8217;2011-01-01&#8242;), priority:1)<br \/>\nExpando b = new Expando(date: Date.parse(&#8216;yyyy-MM-dd&#8217;,&#8217;2011-01-01&#8242;), priority:2)<br \/>\nExpando c = new Expando(date: Date.parse(&#8216;yyyy-MM-dd&#8217;,&#8217;2011-01-02&#8242;), priority:1)<br \/>\nExpando d = new Expando(date: Date.parse(&#8216;yyyy-MM-dd&#8217;,&#8217;2011-01-01&#8242;), priority:3)<\/p>\n<p>def list = [a,d,c,b]<br \/>\n[\/groovy]<\/p>\n<p>If sorting was required on the basis of date only, it was very simple.<\/p>\n<p>[groovy]<br \/>\nlist.sort(it.date)<br \/>\n[\/groovy]<\/p>\n<p>But as per our requirements &#8211; order by date (first level sorting) and priority (second level sorting). We can do something like following.<\/p>\n<p>[groovy]<br \/>\nlist.sort{x,y-&gt;<br \/>\n  if(x.date == y.date){<br \/>\n    x.priority &lt;=&gt; y.priority<br \/>\n  }else{<br \/>\n    x.date &lt;=&gt; y.date<br \/>\n  }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>Well, there could be some better way. If you know please put your comments. But it worked well in my case.<\/p>\n<p>Contact our <a title=\"Groovy Developers\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">groovy developers<\/a> in case you have any query<\/p>\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usually, when we deal with databases, we don&#8217;t face such kind of situation because we query database to get result-set in required order. But let&#8217;s say you have a List of objects that you want to sort on the basis of two fields. Let&#8217;s discuss it with an example. I have a list of Tasks [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":265},"categories":[7],"tags":[504,506,9,4844,505],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2699"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2699"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2699\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}