{"id":17742,"date":"2022-10-26T22:30:26","date_gmt":"2022-10-26T17:00:26","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/"},"modified":"2022-10-26T22:30:26","modified_gmt":"2022-10-26T17:00:26","slug":"solved-sorting-list-of-list-of-long-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/","title":{"rendered":"[Solved] Sorting List of List of Long [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-66235105\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"66235105\" data-parentid=\"66234952\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h2>Use element wise comparator<\/h2>\n<p>Compare 2 lists by<\/p>\n<ol>\n<li>compare in increasing order of index location<\/li>\n<li>if any non-zero value is found, return the result<\/li>\n<li>else compare the size of the lists<\/li>\n<li>sort the lists using this comparator logic<\/li>\n<\/ol>\n<h2>Use per index comparator chain. Based on WJS&#8217;s approach<\/h2>\n<ol>\n<li>Compute the maximum list size across all of the inner lists<\/li>\n<li>Create a chain of comparator for every index from 0 to the computed maximum size<\/li>\n<li>Use this decorated(chain of responsibility) comparator to sort the lists<\/li>\n<\/ol>\n<pre class=\"lang-java prettyprint-override\"><code>import java.util.Arrays;\nimport java.util.Comparator;\nimport java.util.List;\nimport java.util.Objects;\nimport java.util.function.IntFunction;\nimport java.util.stream.Collectors;\nimport java.util.stream.IntStream;\n\npublic class SortNestedList {\n\n    protected List&lt;List&lt;Long&gt;&gt; transform(Long[][] inputArray) {\n        return Arrays.stream(inputArray)\n            .filter(Objects::nonNull) \/\/ filter outer null lists\n            .map(nested -&gt; Arrays.stream(nested)\n                .filter(Objects::nonNull)\n                .collect(Collectors.toList())) \/\/ generate nested list by filtering any null values\n            .collect(Collectors.toList());\n    }\n\n    protected void sortWithElementComparator(List&lt;List&lt;Long&gt;&gt; lists) {\n        lists.sort((a, b) -&gt; {\n            return IntStream.range(0, Math.min(a.size(), b.size())) \/\/ check till end of minimum sized list\n                .map(i -&gt; Long.compare(a.get(i), b.get(i))) \/\/ compare elements at same index\n                .filter(i -&gt; i != 0) \/\/ ignore equal values comparison\n                .findFirst() \/\/ get first non-zero comparison result (unequal)\n                .orElse(Integer.compare(a.size(), b.size())); \/\/ if nothing, then compare size\n        });\n\n        lists.forEach(System.out::println);\n    }\n\n    protected void sortWithDecoratedComparator(List&lt;List&lt;Long&gt;&gt; lists) {\n        \/\/ comparator for an index\n        IntFunction&lt;Comparator&lt;List&lt;Long&gt;&gt;&gt; elementAtIndexComparator =\n            index -&gt; (a, b) -&gt; index &lt; Math.min(a.size(), b.size()) ?\n                Long.compare(a.get(index), b.get(index)) : \/\/ compare elements if index is valid for both lists\n                Integer.compare(a.size(), b.size()); \/\/ compare size if index is invalid for atleast 1 list\n\n        final int maxInnerIndices = lists.stream()\n            .mapToInt(List::size)\n            .max().orElse(0); \/\/ get max inner list size or 0 if empty\n\n        Comparator&lt;List&lt;Long&gt;&gt; listComparator = IntStream.range(0, maxInnerIndices)\n            .mapToObj(elementAtIndexComparator) \/\/ index comparator for every index\n            .reduce(Comparator::thenComparing) \/\/ kind of chain of responsibility \/ decorator\n            .orElse((a, b) -&gt; 0);\n\n        lists.sort(listComparator);\n\n        lists.forEach(System.out::println);\n    }\n\n    protected void sort(Long[][] inputArray, int run) {\n        System.out.println(\"Sort input using direct element comparator: \" + run);\n        sortWithElementComparator(transform(inputArray));\n        System.out.println(\"Sort input using decorated comparator: \" + run);\n        sortWithDecoratedComparator(transform(inputArray));\n    }\n}\n<\/code><\/pre>\n<h2>Test code<\/h2>\n<pre class=\"lang-java prettyprint-override\"><code>    public static void main(String[] args) {\n        final SortNestedList sortNestedList = new SortNestedList();\n        int run = 0;\n\n        Long[][] inputArray = new Long[][]{};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{null}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{null}, {1L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{null, {1L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{1L}, {1L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{2L}, {1L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{2L, 1L}, {1L, 3L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{Long.MAX_VALUE, 1L}, {Long.MIN_VALUE, 3L}};\n        sortNestedList.sort(inputArray, ++run);\n\n        inputArray = new Long[][]{{1L}, {2L, 5L, 6L},\n            {1L, 2L}, {2L}, {1L, 2L, 3L}, {3L, 2L}, {2L, 3L},\n            {1L, 2L, 3L, 4L, 6L}, {1L, 2L, 3L, 4L, 5L},\n            {1L, 2L, 3L, 4L, 6L, 2L}, {1L, 2L, 3L, 4L, 5L, 3L, 5L},\n            {Long.MAX_VALUE, Long.MAX_VALUE, 1L, 5L, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1},\n            {Long.MAX_VALUE, Long.MIN_VALUE, 1L, 5L, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1},\n            {Long.MIN_VALUE}, {Long.MAX_VALUE}};\n        sortNestedList.sort(inputArray, ++run);\n    }\n<\/code><\/pre>\n<p>Thanks to Holger and WJS<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Sorting List of List of Long [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Use element wise comparator Compare 2 lists by compare in increasing order of index location if any non-zero value is found, return the result else compare the size of the lists sort the lists using this comparator logic Use per index comparator chain. Based on WJS&#8217;s approach Compute the maximum list size across all &#8230; <a title=\"[Solved] Sorting List of List of Long [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\" aria-label=\"More on [Solved] Sorting List of List of Long [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,549],"class_list":["post-17742","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-java-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Sorting List of List of Long [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Sorting List of List of Long [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Use element wise comparator Compare 2 lists by compare in increasing order of index location if any non-zero value is found, return the result else compare the size of the lists sort the lists using this comparator logic Use per index comparator chain. Based on WJS&#8217;s approach Compute the maximum list size across all ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-26T17:00:26+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Sorting List of List of Long [closed]\",\"datePublished\":\"2022-10-26T17:00:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\"},\"wordCount\":108,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\",\"java-8\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\",\"name\":\"[Solved] Sorting List of List of Long [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-26T17:00:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Sorting List of List of Long [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Sorting List of List of Long [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Sorting List of List of Long [closed] - JassWeb","og_description":"[ad_1] Use element wise comparator Compare 2 lists by compare in increasing order of index location if any non-zero value is found, return the result else compare the size of the lists sort the lists using this comparator logic Use per index comparator chain. Based on WJS&#8217;s approach Compute the maximum list size across all ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-26T17:00:26+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Sorting List of List of Long [closed]","datePublished":"2022-10-26T17:00:26+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/"},"wordCount":108,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","java-8"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/","name":"[Solved] Sorting List of List of Long [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-26T17:00:26+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-sorting-list-of-list-of-long-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Sorting List of List of Long [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17742"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17742\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}