{"id":888,"date":"2014-02-05T11:55:59","date_gmt":"2014-02-05T18:55:59","guid":{"rendered":"http:\/\/10kdev.net\/?p=888"},"modified":"2014-10-03T13:25:04","modified_gmt":"2014-10-03T20:25:04","slug":"the-zombie-sort-test","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=888","title":{"rendered":"The Zombie Sort Test"},"content":{"rendered":"<p>A great little exercise is implementing a sort comparator.<\/p>\n<p>The problem is this:<\/p>\n<ol>\n<li>Goal: \u00a0Write a concrete implementation to an interface &#8220;Comparator&#8221; \u00a0that sorts an object Zombie by Zombie&#8217;s last name property. \u00a0Comparator has a method &#8220;compare&#8221; that you will override with your implementation.<\/li>\n<li>The \u00a0output is a new list sorted in alphabetical order by last name, ascending.<\/li>\n<li>Provided are the Zombie object and the Service, and any information about Comparator.<\/li>\n<\/ol>\n<p>I fired up my Eclipse editor and wrote it in about 20 minutes. I always use JUnit as a way to run the code &#8212; it&#8217;s a much better technique I feel than writing a main for this fun little test.<\/p>\n<pre><code>\/\/First the main entity class\r\npackage main;\r\n\r\npublic class Zombie {\r\n\r\n\tprivate String firstName;\r\n\tprivate String lastName;\r\n\r\n\tpublic Zombie(String firstName, String lastName) {\r\n\t\tthis.firstName = firstName;\r\n\t\tthis.lastName = lastName;\r\n\t}\r\n\r\n\tpublic String getFirstName() {\r\n\t\treturn firstName;\r\n\t}\r\n\r\n\tpublic void setFirstName(String firstName) {\r\n\t\tthis.firstName = firstName;\r\n\t}\r\n\r\n\tpublic String getLastName() {\r\n\t\treturn lastName;\r\n\t}\r\n\r\n\tpublic void setLastName(String lastName) {\r\n\t\tthis.lastName = lastName;\r\n\t}\r\n\r\n}\r\n\r\n\/\/Here's the service you will use to test your concrete comparator\r\npackage main;\r\n\r\nimport java.util.Collections;\r\nimport java.util.Comparator;\r\nimport java.util.List;\r\n\r\npublic class ZombieService {\r\n\t@SuppressWarnings({ \"unchecked\", \"rawtypes\" })\r\n\tpublic void sortByLastName(List zombies) {\r\n\t\tComparator comp = new ZombieComparator();\r\n\t\tCollections.sort(zombies, comp);\r\n\t\tprintZombie(zombies);\r\n\t}\r\n\r\n\tprivate void printZombie(List zombies) {\r\n\t\tfor (Zombie z : zombies) {\r\n\t\t\tSystem.out.println(z.getLastName());\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\/\/here's the unit test\r\npackage test;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\nimport main.Zombie;\r\nimport main.ZombieService;\r\n\r\nimport org.junit.Test;\r\n\r\npublic class ZombieServiceTest {\r\n\r\n\t@Test\r\n\tpublic void test() {\r\n\t\tList zombieList = new ArrayList();\r\n\t\tzombieList.add(new Zombie(\"Fred\", \"Munster\"));\r\n\t\tzombieList.add(new Zombie(\"Count\", \"Dracula\"));\r\n\t\tzombieList.add(new Zombie(\"Lilly\", \"Adams\"));\r\n\r\n\t\tZombieService zombieService = new ZombieService();\r\n\t\tzombieService.sortByLastName(zombieList);\r\n\t}\r\n\r\n}\r\n\r\n<\/code>\r\n<code>\/\/Here's the comparator implementation  THIS IS THE SOLUTION!\r\npackage main;\r\n\r\nimport java.util.Comparator;\r\n\r\n@SuppressWarnings(\"rawtypes\")\r\npublic class ZombieComparator implements Comparator {\r\n\r\n\t@Override\r\n\tpublic int compare(Object arg0, Object arg1) {\r\n\t\tZombie o0 = (Zombie) arg0;\r\n\t\tZombie o1 = (Zombie) arg1;\r\n\t\treturn o0.getLastName().compareTo(o1.getLastName());\r\n\t}\r\n}\r\n\r\n<\/code><\/pre>\n<p>The output:<\/p>\n<p style=\"padding-left: 30px;\">Adams<br \/>\nDracula<br \/>\nMunster<\/p>\n<hr \/>\n<p>I am thinking of doing more algorithm studies at a code meetup group I started, which would focus on the fundamental problem solving skills which we may or may not use at work. The idea would be to bring any compiler (maybe groovy, or clojure, ruby, pearl etc.). How about solving the fizzbuzz problem in SQL? What about Fibonacci with QBasic? Many languages, many people, many approaches, many funs! I&#8217;ll give it some thought &#8230;<\/p>\n<p>I added the project to Bitbucket in the Zombie folder and its Eclipse files:<\/p>\n<p><a href=\"https:\/\/bitbucket.org\/bilmowry\/10kdev\/src\/29a298277637ce4ae86337a0a65a6d98fe04d6cd\/Zombie\/?at=master\">Zombie<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A great little exercise is implementing a sort comparator. The problem is this: Goal: \u00a0Write a concrete implementation to an interface &#8220;Comparator&#8221; \u00a0that sorts an object Zombie by Zombie&#8217;s last name property. \u00a0Comparator has a method &#8220;compare&#8221; that you will override with your implementation. The \u00a0output is a new list sorted in alphabetical order by [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/888"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=888"}],"version-history":[{"count":14,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/888\/revisions"}],"predecessor-version":[{"id":1272,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/888\/revisions\/1272"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}