{"id":23791,"date":"2024-03-16T02:01:30","date_gmt":"2024-03-16T02:01:30","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/"},"modified":"2024-03-22T02:07:41","modified_gmt":"2024-03-22T02:07:41","slug":"how-to-use-the-sort-function-to-sort-a-2d-array-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/","title":{"rendered":"How to use the sort function to sort a 2D array in C++?"},"content":{"rendered":"<p>To sort a two-dimensional array in C++, you can use the sort function along with a custom comparison function. Here is an example code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;algorithm&gt;<\/span><\/span>\r\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">namespace<\/span> std;\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">bool<\/span> <span class=\"hljs-title\">compareArrays<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span>* arr1, <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span>* arr2, <span class=\"hljs-type\">int<\/span> size)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; size; i++) {\r\n        <span class=\"hljs-keyword\">if<\/span> (arr1[i] &lt; arr2[i]) {\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">true<\/span>;\r\n        } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (arr1[i] &gt; arr2[i]) {\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">false<\/span>;\r\n        }\r\n    }\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">false<\/span>;\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n    <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span> rows = <span class=\"hljs-number\">3<\/span>;\r\n    <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span> cols = <span class=\"hljs-number\">4<\/span>;\r\n    <span class=\"hljs-type\">int<\/span> arr[rows][cols] = {{<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>}, {<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>}, {<span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">12<\/span>}};\r\n\r\n    <span class=\"hljs-built_in\">sort<\/span>(&amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>], &amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>] + rows * cols, [&amp;](<span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span>&amp; a, <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">int<\/span>&amp; b) {\r\n        <span class=\"hljs-type\">int<\/span> row1 = (<span class=\"hljs-type\">int<\/span>)(&amp;a - &amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>]) \/ cols;\r\n        <span class=\"hljs-type\">int<\/span> col1 = (<span class=\"hljs-type\">int<\/span>)(&amp;a - &amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>]) % cols;\r\n        <span class=\"hljs-type\">int<\/span> row2 = (<span class=\"hljs-type\">int<\/span>)(&amp;b - &amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>]) \/ cols;\r\n        <span class=\"hljs-type\">int<\/span> col2 = (<span class=\"hljs-type\">int<\/span>)(&amp;b - &amp;arr[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-number\">0<\/span>]) % cols;\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">compareArrays<\/span>(arr[row1], arr[row2], cols);\r\n    });\r\n\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; rows; i++) {\r\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> j = <span class=\"hljs-number\">0<\/span>; j &lt; cols; j++) {\r\n            cout &lt;&lt; arr[i][j] &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\r\n        }\r\n        cout &lt;&lt; endl;\r\n    }\r\n\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>In this example, we begin by defining a compareArrays function to compare the relative sizes of two one-dimensional arrays. Then, in the main function, we use the sort function to sort a two-dimensional array. Here, we specify a custom comparison function that compares the elements of the two-dimensional array based on row priority.<\/p>\n<p>Please note that in order to obtain the row and column indices of elements in the comparison function, we use pointer arithmetic to calculate the position of elements in the two-dimensional array.<\/p>\n<p>Finally, we use two nested loops to iterate through and output the sorted two-dimensional array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To sort a two-dimensional array in C++, you can use the sort function along with a custom comparison function. Here is an example code: #include &lt;iostream&gt; #include &lt;algorithm&gt; using namespace std; bool compareArrays(const int* arr1, const int* arr2, int size) { for (int i = 0; i &lt; size; i++) { if (arr1[i] &lt; arr2[i]) [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-23791","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use the sort function to sort a 2D array in C++? - Blog - Silicon Cloud<\/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:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the sort function to sort a 2D array in C++?\" \/>\n<meta property=\"og:description\" content=\"To sort a two-dimensional array in C++, you can use the sort function along with a custom comparison function. Here is an example code: #include &lt;iostream&gt; #include &lt;algorithm&gt; using namespace std; bool compareArrays(const int* arr1, const int* arr2, int size) { for (int i = 0; i &lt; size; i++) { if (arr1[i] &lt; arr2[i]) [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T02:01:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T02:07:41+00:00\" \/>\n<meta name=\"author\" content=\"Jackson Davis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jackson Davis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\"},\"author\":{\"name\":\"Jackson Davis\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/55a10b8b0457c35884c25677889ad350\"},\"headline\":\"How to use the sort function to sort a 2D array in C++?\",\"datePublished\":\"2024-03-16T02:01:30+00:00\",\"dateModified\":\"2024-03-22T02:07:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\"},\"wordCount\":137,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\",\"name\":\"How to use the sort function to sort a 2D array in C++? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T02:01:30+00:00\",\"dateModified\":\"2024-03-22T02:07:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use the sort function to sort a 2D array in C++?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/55a10b8b0457c35884c25677889ad350\",\"name\":\"Jackson Davis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2fdb47d6df1226e92380d96973782572a97b0675d098bb914410dec348eb5d29?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2fdb47d6df1226e92380d96973782572a97b0675d098bb914410dec348eb5d29?s=96&d=mm&r=g\",\"caption\":\"Jackson Davis\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/jacksondavis\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use the sort function to sort a 2D array in C++? - Blog - Silicon Cloud","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:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/","og_locale":"en_US","og_type":"article","og_title":"How to use the sort function to sort a 2D array in C++?","og_description":"To sort a two-dimensional array in C++, you can use the sort function along with a custom comparison function. Here is an example code: #include &lt;iostream&gt; #include &lt;algorithm&gt; using namespace std; bool compareArrays(const int* arr1, const int* arr2, int size) { for (int i = 0; i &lt; size; i++) { if (arr1[i] &lt; arr2[i]) [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T02:01:30+00:00","article_modified_time":"2024-03-22T02:07:41+00:00","author":"Jackson Davis","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Jackson Davis","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/"},"author":{"name":"Jackson Davis","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/55a10b8b0457c35884c25677889ad350"},"headline":"How to use the sort function to sort a 2D array in C++?","datePublished":"2024-03-16T02:01:30+00:00","dateModified":"2024-03-22T02:07:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/"},"wordCount":137,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/","name":"How to use the sort function to sort a 2D array in C++? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T02:01:30+00:00","dateModified":"2024-03-22T02:07:41+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-the-sort-function-to-sort-a-2d-array-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use the sort function to sort a 2D array in C++?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/55a10b8b0457c35884c25677889ad350","name":"Jackson Davis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2fdb47d6df1226e92380d96973782572a97b0675d098bb914410dec348eb5d29?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2fdb47d6df1226e92380d96973782572a97b0675d098bb914410dec348eb5d29?s=96&d=mm&r=g","caption":"Jackson Davis"},"url":"https:\/\/www.silicloud.com\/blog\/author\/jacksondavis\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/23791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=23791"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/23791\/revisions"}],"predecessor-version":[{"id":57789,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/23791\/revisions\/57789"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=23791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=23791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=23791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}