{"id":65389,"date":"2025-05-03T17:10:34","date_gmt":"2025-05-03T09:10:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65389\/"},"modified":"2025-05-03T17:10:34","modified_gmt":"2025-05-03T09:10:34","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65389\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u6570\u7ec4\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u65e0\u9700\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u53ef\u5b9e\u73b0 comparator \u63a5\u53e3\u5e76\u91cd\u5199 compare() \u65b9\u6cd5\uff1b\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\u5305\u62ec collections.sort()\u3001arrays.parallelsort() \u548c timsort\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111223092839061.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays \u7c7b\u7684 sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002sort() \u65b9\u6cd5\u4f1a\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c31\u5730\u6392\u5e8f\uff0c\u65e0\u9700\u521b\u5efa\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528 sort() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 2, 8, 3, 1};\nArrays.sort(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>String[] strArr = {\"apple\", \"banana\", \"cherry\"};\nArrays.sort(strArr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0csort() \u65b9\u6cd5\u4f1a\u6839\u636e\u5143\u7d20\u7684\u81ea\u7136\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982 int\u3001double\uff09\uff0c\u8fd9\u662f\u9ed8\u8ba4\u7684\u6392\u5e8f\u987a\u5e8f\u3002\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u9700\u8981\u5b9e\u73b0 Comparable \u63a5\u53e3\u5e76\u91cd\u5199 compareTo() \u65b9\u6cd5\u6765\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a<\/strong><\/p>\n<p>\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u9700\u8981\u5b9e\u73b0 Comparator \u63a5\u53e3\u5e76\u91cd\u5199 compare() \u65b9\u6cd5\u3002compare() \u65b9\u6cd5\u5e94\u8fd4\u56de\u4ee5\u4e0b\u503c\uff1a<\/p>\n<ul>\n<li> <strong>-1\uff1a<\/strong> \u5982\u679c\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4e4b\u524d<\/li>\n<li> <strong>0\uff1a<\/strong> \u5982\u679c\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49<\/li>\n<li> <strong>1\uff1a<\/strong> \u5982\u679c\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4e4b\u540e<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528 Comparator \u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>Arrays.sort(arr, new Comparator&lt;Integer&gt;() {\n    @Override\n    public int compare(Integer a, Integer b) {\n        \/\/ \u81ea\u5b9a\u4e49\u6392\u5e8f\u903b\u8f91\n    }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86 sort() \u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<ul>\n<li> <strong>Collections.sort()\uff1a<\/strong> \u9002\u7528\u4e8e List \u548c Set \u7b49\u96c6\u5408\uff0c\u4f7f\u7528 Comparator \u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f<\/li>\n<li> <strong>Arrays.parallelSort()\uff1a<\/strong> \u5bf9\u5927\u578b\u6570\u7ec4\u8fdb\u884c\u5e76\u884c\u6392\u5e8f<\/li>\n<li> <strong>TimSort\uff1a<\/strong> Java \u4f7f\u7528\u7684\u4e00\u79cd\u6df7\u5408\u6392\u5e8f\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\uff0c\u6548\u7387\u9ad8\u4e14\u7a33\u5b9a<\/li>\n<\/ul>\n<p>\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u60a8\u6709\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u6570\u7ec4\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 \u4e2d\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u65e0\u9700\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u53ef\u5b9e\u73b0 comparator \u63a5\u53e3\u5e76\u91cd\u5199 compare() \u65b9\u6cd5\uff1b\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\u5305\u62ec collections.sort()\u3001arrays.parallelsort() \u548c timsort\u3002 Java \u4e2d\u6570\u7ec4\u6392\u5e8f \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays \u7c7b\u7684 sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002sort() \u65b9\u6cd5\u4f1a\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c31\u5730\u6392\u5e8f\uff0c\u65e0\u9700\u521b\u5efa\u65b0\u7684\u6570\u7ec4\u3002 \u4f7f\u7528 sort() \u65b9\u6cd5\uff1a int[] arr = {5, 2, 8, 3, 1}; Arrays.sort(arr); \u767b\u5f55\u540e\u590d\u5236 String[] strArr = {&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;}; Arrays.sort(strArr); \u767b\u5f55\u540e\u590d\u5236 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0csort() \u65b9\u6cd5\u4f1a\u6839\u636e\u5143\u7d20\u7684\u81ea\u7136\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982 int\u3001double\uff09\uff0c\u8fd9\u662f\u9ed8\u8ba4\u7684\u6392\u5e8f\u987a\u5e8f\u3002\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u9700\u8981\u5b9e\u73b0 Comparable \u63a5\u53e3\u5e76\u91cd\u5199 compareTo() \u65b9\u6cd5\u6765\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a \u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u9700\u8981\u5b9e\u73b0 Comparator \u63a5\u53e3\u5e76\u91cd\u5199 compare() [&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-65389","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65389","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=65389"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65389\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}