{"id":64177,"date":"2025-05-03T10:07:57","date_gmt":"2025-05-03T02:07:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64177\/"},"modified":"2025-05-03T10:07:57","modified_gmt":"2025-05-03T02:07:57","slug":"java%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e7%94%a8sort-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64177\/","title":{"rendered":"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u7528sort"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5bf9 java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f\u6839\u636e\u884c\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u6392\u5e8f\u540e\u518d\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u6839\u636e\u5217\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u8f6c\u7f6e\uff0c\u6309\u884c\u6392\u5e8f\uff0c\u518d\u8f6c\u7f6e\u56de\u6765\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110217154580445.jpg\" class=\"aligncenter\" title=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u7528sort\u63d2\u56fe\" alt=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u7528sort\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u4e2d\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u91c7\u7528\u66f4\u590d\u6742\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u4e24\u4e2a\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<p><strong>1. \u6839\u636e\u884c\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 java.util.Arrays.stream() \u5e2e\u52a9\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002<\/li>\n<li>\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff08\u4f8b\u5982\uff0c\u4f7f\u7528 Arrays.sort()\uff09\u3002<\/li>\n<li>\u5c06\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[][] array = {\n  {1, 3},\n  {4, 2},\n  {7, 5}\n};\n\n\/\/ \u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\nint[] flatArray = Arrays.stream(array).flatMapToInt(Arrays::stream).toArray();\n\n\/\/ \u6392\u5e8f\u4e00\u7ef4\u6570\u7ec4\nArrays.sort(flatArray);\n\n\/\/ \u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\nint[][] sortedArray = new int[array.length][array[0].length];\nint index = 0;\nfor (int i = 0; i &lt; array.length; i++) {\n  for (int j = 0; j &lt; array[0].length; j++) {\n    sortedArray[i][j] = flatArray[index++];\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u6839\u636e\u5217\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u7f6e\uff0c\u5373\u4ea4\u6362\u884c\u548c\u5217\u3002<\/li>\n<li>\u5bf9\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u6839\u636e\u884c\u6392\u5e8f\uff08\u5982\u4e0a\u6587\u6240\u8ff0\uff09\u3002<\/li>\n<li>\u518d\u5c06\u6570\u7ec4\u8f6c\u7f6e\u56de\u6765\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[][] array = {\n  {1, 3},\n  {4, 2},\n  {7, 5}\n};\n\n\/\/ \u8f6c\u7f6e\u6570\u7ec4\nint[][] transposedArray = new int[array[0].length][array.length];\nfor (int i = 0; i &lt; array.length; i++) {\n  for (int j = 0; j &lt; array[0].length; j++) {\n    transposedArray[j][i] = array[i][j];\n  }\n}\n\n\/\/ \u6839\u636e\u884c\u6392\u5e8f\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\nfor (int[] row : transposedArray) {\n  Arrays.sort(row);\n}\n\n\/\/ \u8f6c\u7f6e\u56de\u539f\u59cb\u6570\u7ec4\nfor (int i = 0; i &lt; array.length; i++) {\n  for (int j = 0; j &lt; array[0].length; j++) {\n    array[i][j] = transposedArray[j][i];\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u7528sort\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5bf9 java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f\u6839\u636e\u884c\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u6392\u5e8f\u540e\u518d\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u6839\u636e\u5217\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u8f6c\u7f6e\uff0c\u6309\u884c\u6392\u5e8f\uff0c\u518d\u8f6c\u7f6e\u56de\u6765\u3002 \u5982\u4f55\u4f7f\u7528 Java \u4e2d\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u91c7\u7528\u66f4\u590d\u6742\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u4e24\u4e2a\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a 1. \u6839\u636e\u884c\u6392\u5e8f \u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 java.util.Arrays.stream() \u5e2e\u52a9\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002 \u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff08\u4f8b\u5982\uff0c\u4f7f\u7528 Arrays.sort()\uff09\u3002 \u5c06\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a int[][] array = { {1, 3}, {4, 2}, {7, 5} }; \/\/ \u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4 int[] flatArray = Arrays.stream(array).flatMapToInt(Arrays::stream).toArray(); \/\/ \u6392\u5e8f\u4e00\u7ef4\u6570\u7ec4 Arrays.sort(flatArray); \/\/ \u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4 int[][] sortedArray = new int[array.length][array[0].length]; int index = 0; for (int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64177","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64177"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64177\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}