{"id":8507,"date":"2012-09-25T01:32:42","date_gmt":"2012-09-24T20:02:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8507"},"modified":"2022-01-13T16:47:56","modified_gmt":"2022-01-13T11:17:56","slug":"sorting-list-with-nulls-as-higher-value","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sorting-list-with-nulls-as-higher-value\/","title":{"rendered":"Sorting list with nulls as higher value"},"content":{"rendered":"<p>I had a list of Strings and wanted to sort them but pay special attention to null values. See the default sorting in demonstration :<\/p>\n<p>[groovy]<\/p>\n<p>List&amp;lt;String&amp;gt; animals=[&amp;quot;Monkey&amp;quot;, null, &amp;quot;Ape&amp;quot;, null, &amp;quot;Tiger&amp;quot;, &amp;quot;Lion&amp;quot;]<\/p>\n<p>println animals.sort()              \/\/ [null, null, Ape, Lion, Monkey, Tiger]<br \/>\nprintln animals.sort().reverse()\t\/\/ [Tiger, Monkey, Lion, Ape, null, null] <\/p>\n<p>[\/groovy]<\/p>\n<p style='padding-bottom:10px;'>\nYou can see that nulls are lower in default groovy sorting.\n<\/p>\n<p style='padding-bottom:10px;'>\nWhat if we wanted to treat null as higher. In other words: Sort null values at the bottom in case of Ascending order and Sort null values at the top in case of Descending.<\/p>\n<p><p style='padding-bottom:10px;'>\n<a href=\"http:\/\/mvnrepository.com\/artifact\/commons-collections\/commons-collections\/3.2.1\">Apache commons-collections jar file <\/a> contains a utility Comparator class : NullComparator. This class can be used to change the high\/low interpretation of null values in sorting. Here is the sample code to see the NullComparator in action. (Note that this jar file is already included in grails library. Use grab to include this jar file when you are running code in groovy console).<\/p>\n<p>\n[groovy]<\/p>\n<p>@Grab(group=&#8217;commons-collections&#8217;, module=&#8217;commons-collections&#8217;, version=&#8217;3.2.1&#8242;)<br \/>\nimport org.apache.commons.collections.comparators.NullComparator<br \/>\n\/\/ public NullComparator(boolean nullsAreHigh)<\/p>\n<p>def nullHigh = new NullComparator(true)<br \/>\nprintln animals.sort{a, b-&amp;gt; nullHigh.compare(a, b)}  \/\/ [Ape, Lion, Monkey, Tiger, null, null]<br \/>\nprintln animals.sort{b, a-&amp;gt; nullHigh.compare(a, b)}  \/\/ [null, null, Tiger, Monkey, Lion, Ape]<\/p>\n<p>def nullLow = new NullComparator(false)<br \/>\nprintln animals.sort{a, b-&amp;gt; nullLow.compare(a, b)} \/\/ same as animals.sort()<br \/>\nprintln animals.sort{b, a-&amp;gt; nullLow.compare(a, b)} \/\/ same as animals.sort().reverse()<\/p>\n<p>[\/groovy]<\/p>\n<p style='padding-bottom:10px;'>\nUseful Links :<\/p>\n<p><a href=\"http:\/\/mvnrepository.com\/artifact\/commons-collections\/commons-collections\/3.2.1\">http:\/\/mvnrepository.com\/artifact\/commons-collections\/commons-collections\/3.2.1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a list of Strings and wanted to sort them but pay special attention to null values. See the default sorting in demonstration : [groovy] List&amp;lt;String&amp;gt; animals=[&amp;quot;Monkey&amp;quot;, null, &amp;quot;Ape&amp;quot;, null, &amp;quot;Tiger&amp;quot;, &amp;quot;Lion&amp;quot;] println animals.sort() \/\/ [null, null, Ape, Lion, Monkey, Tiger] println animals.sort().reverse() \/\/ [Tiger, Monkey, Lion, Ape, null, null] [\/groovy] You can see [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[1],"tags":[1054,68,1056,1055,1053],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8507"}],"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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8507"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8507\/revisions"}],"predecessor-version":[{"id":54655,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8507\/revisions\/54655"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}