{"id":65820,"date":"2025-05-03T15:16:01","date_gmt":"2025-05-03T07:16:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65820\/"},"modified":"2025-05-03T15:16:01","modified_gmt":"2025-05-03T07:16:01","slug":"java%e6%80%8e%e4%b9%88%e5%af%b9%e6%95%b0%e7%bb%84%e8%bf%9b%e8%a1%8c%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65820\/","title":{"rendered":"java\u600e\u4e48\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1aarrays.sort() \u65b9\u6cd5\uff1a\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u3002collection.sort() \u65b9\u6cd5\uff1a\u5bf9\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u3002\u624b\u52a8\u6392\u5e8f\uff1a\u9002\u5408\u5c0f\u578b\u6570\u7ec4\uff0c\u6d89\u53ca\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\u3002\u5916\u90e8\u6392\u5e8f\u5e93\uff1a\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111311333823258.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<p><strong>1. Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u662f\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u7684\u4e3b\u6d41\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u7ec4\u5927\u5c0f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {5, 2, 8, 3, 1};\nArrays.sort(numbers); \/\/ \u6309\u5347\u5e8f\u6392\u5e8f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. Collection.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u53ef\u7528\u4e8e\u5bf9\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u524d\u63d0\u662f\u8fd9\u4e9b\u5bf9\u8c61\u5b9e\u73b0\u4e86 Comparable \u63a5\u53e3\u3002<\/p>\n<pre>List&lt;String&gt; names = new ArrayList&lt;&gt;();\nnames.add(\"John\");\nnames.add(\"Bob\");\nnames.add(\"Alice\");\nnames.sort(Comparator.naturalOrder()); \/\/ \u6309\u81ea\u7136\u987a\u5e8f\u6392\u5e8f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u624b\u52a8\u6392\u5e8f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5c0f\u578b\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u624b\u52a8\u6392\u5e8f\u3002\u8fd9\u6d89\u53ca\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\uff0c\u76f4\u5230\u6570\u7ec4\u6709\u5e8f\u3002<\/p>\n<pre>int[] numbers = {5, 2, 8, 3, 1};\nfor (int i = 0; i &lt; numbers.length; i++) {\n    for (int j = i + 1; j &lt; numbers.length; j++) {\n        if (numbers[i] &gt; numbers[j]) {\n            int temp = numbers[i];\n            numbers[i] = numbers[j];\n            numbers[j] = temp;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u5916\u90e8\u6392\u5e8f\u5e93<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5916\u90e8\u6392\u5e8f\u5e93\uff0c\u4f8b\u5982 Apache Commons Lang\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5373\u4f7f\u5bf9\u4e8e\u5185\u5b58\u4e2d\u65e0\u6cd5\u5bb9\u7eb3\u7684\u6570\u7ec4\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n<pre>import org.apache.commons.lang3.ArrayUtils;\nint[] numbers = {5, 2, 8, 3, 1};\nArrayUtils.sort(numbers); \/\/ \u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f:<\/strong><\/p>\n<ul>\n<li>\u6392\u5e8f\u540e\uff0c\u539f\u59cb\u6570\u7ec4\u7684\u5185\u5bb9\u5c06\u88ab\u4fee\u6539\u3002<\/li>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n<li>\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u6392\u5e8f\u884c\u4e3a\u53d6\u51b3\u4e8e\u6240\u4f7f\u7528\u7684\u6bd4\u8f83\u5668\u3002<\/li>\n<li>\u5728\u51b3\u5b9a\u4f7f\u7528\u54ea\u79cd\u6392\u5e8f\u65b9\u6cd5\u65f6\uff0c\u6570\u7ec4\u5927\u5c0f\u3001\u6570\u636e\u7c7b\u578b\u548c\u6027\u80fd\u8981\u6c42\u90fd\u662f\u5173\u952e\u56e0\u7d20\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\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>java\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u7ec4\u6392\u5e8f\u65b9\u6cd5\uff1aarrays.sort() \u65b9\u6cd5\uff1a\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u3002collection.sort() \u65b9\u6cd5\uff1a\u5bf9\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u3002\u624b\u52a8\u6392\u5e8f\uff1a\u9002\u5408\u5c0f\u578b\u6570\u7ec4\uff0c\u6d89\u53ca\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\u3002\u5916\u90e8\u6392\u5e8f\u5e93\uff1a\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002 \u5982\u4f55\u5bf9 Java \u6570\u7ec4\u8fdb\u884c\u6392\u5e8f Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a 1. Arrays.sort() \u65b9\u6cd5 \u8fd9\u662f\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u7684\u4e3b\u6d41\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u7ec4\u5927\u5c0f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = {5, 2, 8, 3, 1}; Arrays.sort(numbers); \/\/ \u6309\u5347\u5e8f\u6392\u5e8f \u767b\u5f55\u540e\u590d\u5236 2. Collection.sort() \u65b9\u6cd5 \u6b64\u65b9\u6cd5\u53ef\u7528\u4e8e\u5bf9\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u524d\u63d0\u662f\u8fd9\u4e9b\u5bf9\u8c61\u5b9e\u73b0\u4e86 Comparable \u63a5\u53e3\u3002 List&lt;String&gt; names = new ArrayList&lt;&gt;(); names.add(&#8220;John&#8221;); names.add(&#8220;Bob&#8221;); names.add(&#8220;Alice&#8221;); names.sort(Comparator.naturalOrder()); \/\/ \u6309\u81ea\u7136\u987a\u5e8f\u6392\u5e8f \u767b\u5f55\u540e\u590d\u5236 3. \u624b\u52a8\u6392\u5e8f \u5bf9\u4e8e\u5c0f\u578b\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u624b\u52a8\u6392\u5e8f\u3002\u8fd9\u6d89\u53ca\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\uff0c\u76f4\u5230\u6570\u7ec4\u6709\u5e8f\u3002 int[] numbers = {5, 2, 8, 3, 1}; for [&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-65820","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65820","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=65820"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65820\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}