{"id":448,"date":"2015-12-30T23:07:28","date_gmt":"2015-12-30T21:07:28","guid":{"rendered":"http:\/\/kra.lc\/blog\/?p=448"},"modified":"2015-12-30T23:10:43","modified_gmt":"2015-12-30T21:10:43","slug":"generic-arraycomparator-for-comparable-java-types","status":"publish","type":"post","link":"https:\/\/kra.lc\/blog\/2015\/12\/generic-arraycomparator-for-comparable-java-types\/","title":{"rendered":"Generic ArrayComparator for Comparable Java types"},"content":{"rendered":"<p>Another day, another post! <a href=\"https:\/\/kra.lc\/blog\/2015\/12\/java-thread-local-servlet-cookie-filter\/\">The marathon continues.<\/a> After watching &#8220;Star Wars: The Force Awakens&#8221; <a href=\"https:\/\/kra.lc\/blog\/2015\/12\/java-file-manager-servlet\/\">yesterday<\/a>, let&#8217;s speak a few words about <a href=\"https:\/\/opensource.org\/\">Open Source<\/a>. Since I began releasing <a href=\"http:\/\/kra.lc\/projects\">my projects<\/a>, most of it has been released on a dedicated website or on this blog. Meanwhile GitHub is the main source for free and open source code, therefore I decided to create an own profile at: <a href=\"https:\/\/github.com\/kristian\"><strong>https:\/\/github.com\/kristian<\/strong><\/a> <em>(yay! xD)<\/em><\/p>\n<p>I think GitHub is a great way to contribute to Open Source and collaborate between individuals. I&#8217;m going to release much of my stuff on GitHub, but add a few more words about it here in my blog. Some smaller snippets will stay though, such as one thing, that I was surprised is not part of the Java standard libraries yet: A generic <code>ArrayComparator<\/code> for <code>Comparable<\/code> Java types.<\/p>\n<p><!--more--><\/p>\n<p>The task was quite simple, I wanted to sort a <code>Collection<\/code> of arrays in Java. To do so, I wanted to use a <code>TreeMap<\/code> to sort it&#8217;s elements, while adding elements to the <code>SortedMap<\/code>. Unfortunately arrays in Java do not implement the <code>Compareable<\/code> interface by default. So a <code>Comparator<\/code> was needed. Much to my surpise, I found no <code>ArrayComparator<\/code>, neither in the standard Java library code, nor on any source on the internet. Here&#8217;s my implementation of a generic <code>ArrayComparator<\/code> class:<\/p>\n<pre class=\"syntax java\" title=\"ArrayComparator.java\">\r\npublic class ArrayComparator&lt;T extends Comparable&lt;T&gt;&gt; implements Comparator&lt;T[]&gt; {\r\n\t@Override public int compare(T[] arrayA, T[] arrayB) {\r\n\t\tif(arrayA==arrayB) return 0; int compare;\r\n\t\tfor(int index=0;index&lt;arrayA.length;index++)\r\n\t\t\tif(index&lt;arrayB.length) {\r\n\t\t\t\tif((compare=arrayA[index].compareTo(arrayB[index]))!=0)\r\n\t\t\t\t\treturn compare;\r\n\t\t\t} else return 1; \/\/first array is longer\r\n\t\tif(arrayA.length==arrayB.length)\r\n\t\t\t   return 0; \/\/arrays are equal\r\n\t\telse return -1; \/\/first array is shorter \r\n\t}\r\n}\r\n<\/pre>\n<p>There is only one prerequisite for the comparator though, the generic base type of the array must implement the <code>Comparable<\/code> interface. Now sorting multi-dimensional arrays, <code>Collections<\/code>, or <code>(Sorted)Maps<\/code> really becomes a piece of cake! Let me share a few examples:<\/p>\n<pre class=\"syntax java\" title=\"Multi-Dimensional Arrays\">\r\nString[][] sorted = new String[][]{{&quot;A&quot;,&quot;B&quot;},{&quot;B&quot;,&quot;C&quot;},{&quot;A&quot;,&quot;C&quot;}};\r\nArrays.sort(sorted, new ArrayComparator&lt;&gt;());\r\n<\/pre>\n<pre class=\"syntax java\" title=\"Collections\">\r\nList&lt;String[]&gt; sorted = new ArrayList&lt;&gt;();\r\nsorted.add(new String[]{&quot;A&quot;,&quot;B&quot;});\r\nsorted.add(new String[]{&quot;B&quot;,&quot;C&quot;});\r\nsorted.add(new String[]{&quot;A&quot;,&quot;C&quot;});\r\nsorted.sort(new ArrayComparator&lt;&gt;());\r\n<\/pre>\n<pre class=\"syntax java\" title=\"(Sorted)Maps\">\r\nMap&lt;String[],Object&gt; sorted = new TreeMap&lt;&gt;(new ArrayComparator&lt;&gt;());\r\nsorted.put(new String[]{&quot;A&quot;,&quot;B&quot;}, new Object());\r\nsorted.put(new String[]{&quot;B&quot;,&quot;C&quot;}, new Object());\r\nsorted.put(new String[]{&quot;A&quot;,&quot;C&quot;}, new Object());\r\n<\/pre>\n<p>I hope you find this little code snippet useful. Stay tuned for the forth post of my little coding marathon <em>tomorrow<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another day, another post! The marathon continues. After watching &#8220;Star Wars: The Force Awakens&#8221; yesterday, let&#8217;s speak a few words about Open Source. Since I began releasing my projects, most of it has been released on a dedicated website or on this blog. Meanwhile GitHub is the main source for free and open source code, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[200,202,203,201,204,53,5],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-release","tag-array","tag-arraycomparator","tag-comparable","tag-comparator","tag-compare","tag-generic","tag-java"],"_links":{"self":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":26,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/448\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}