{"id":37360,"date":"2024-11-26T09:15:15","date_gmt":"2024-11-26T01:15:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37360\/"},"modified":"2024-11-26T09:15:15","modified_gmt":"2024-11-26T01:15:15","slug":"java%e6%80%8e%e4%b9%88%e7%bb%99%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37360\/","title":{"rendered":"java\u600e\u4e48\u7ed9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u53ef\u4ee5\u901a\u8fc7 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u65b9\u6cd5\u91c7\u7528\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u4ee5\u7ebf\u6027\u5bf9\u6570\u590d\u6742\u5ea6\u5bf9\u539f\u59cb\u6570\u7ec4\u539f\u4f4d\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\uff0c\u53ef\u4ee5\u4f7f\u7528 comparator \u63a5\u53e3\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304334217987.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7ed9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7ed9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u5f00\u95e8\u89c1\u5c71\uff1a<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u4ee5\u7ebf\u6027\u5bf9\u6570\u590d\u6742\u5ea6\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u539f\u4f4d\u6392\u5e8f\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<ul>\n<li>\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\uff08\u4f8b\u5982\uff0c\u5feb\u901f\u6392\u5e8f\uff09<\/li>\n<li>\u5c06\u6570\u7ec4\u5143\u7d20\u4e0e\u5f7c\u6b64\u6bd4\u8f83<\/li>\n<li>\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u79fb\u5230\u8f83\u5927\u7684\u5143\u7d20\u524d\u9762<\/li>\n<\/ul>\n<p><strong>2. \u6392\u5e8f\u539f\u59cb\u6570\u7ec4<\/strong><\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u4e0d\u4f1a\u521b\u5efa\u6570\u7ec4\u7684\u65b0\u526f\u672c\uff0c\u800c\u662f\u5bf9\u5176\u539f\u59cb\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u6570\u7ec4\u88ab\u6392\u5e8f\uff0c\u4efb\u4f55\u5bf9\u5176\u5143\u7d20\u8fdb\u884c\u7684\u4fee\u6539\u90fd\u5c06\u53cd\u6620\u5728\u539f\u59cb\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>3. \u6bd4\u8f83\u5668\u63a5\u53e3<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 Comparator \u63a5\u53e3\u6765\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002Comparator \u63d0\u4f9b\u4e00\u4e2a compare() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff1a<\/p>\n<ul>\n<li>\u8d1f\u6570\u8868\u793a\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u6392\u5728\u524d<\/li>\n<li>\u6b63\u6570\u8868\u793a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u5e94\u8be5\u6392\u5728\u524d<\/li>\n<li>\u96f6\u8868\u793a\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>\/\/ \u539f\u59cb\u6570\u7ec4\nint[] numbers = {5, 2, 8, 1, 4};\n\n\/\/ \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\nArrays.sort(numbers);\n\n\/\/ \u6253\u5370\u6392\u5e8f\u540e\u7684\u6570\u7ec4\nSystem.out.println(Arrays.toString(numbers)); \/\/ \u8f93\u51fa\uff1a [1, 2, 4, 5, 8]\n\n\/\/ \u4f7f\u7528\u6bd4\u8f83\u5668\u5bf9\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\nArrays.sort(objects, new MyComparator());<\/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 Arrays.sort() \u65b9\u6cd5\u4e4b\u5916\uff0cJava \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>Collections.sort()\uff1a\u9002\u7528\u4e8e List \u548c Set \u7b49\u96c6\u5408<\/li>\n<li>Stream.sorted()\uff1a\u9002\u7528\u4e8e\u6d41<\/li>\n<li>PriorityQueue\uff1a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u6570\u636e\u7ed3\u6784\uff0c\u5728\u63d2\u5165\u65f6\u81ea\u52a8\u6392\u5e8f\u5143\u7d20<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7ed9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u53ef\u4ee5\u901a\u8fc7 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u65b9\u6cd5\u91c7\u7528\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u4ee5\u7ebf\u6027\u5bf9\u6570\u590d\u6742\u5ea6\u5bf9\u539f\u59cb\u6570\u7ec4\u539f\u4f4d\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\uff0c\u53ef\u4ee5\u4f7f\u7528 comparator \u63a5\u53e3\u3002 Java \u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f \u5f00\u95e8\u89c1\u5c71\uff1a \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002 \u8be6\u7ec6\u89e3\u91ca\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5 Arrays.sort() \u65b9\u6cd5\u4ee5\u7ebf\u6027\u5bf9\u6570\u590d\u6742\u5ea6\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u539f\u4f4d\u6392\u5e8f\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff1a \u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\uff08\u4f8b\u5982\uff0c\u5feb\u901f\u6392\u5e8f\uff09 \u5c06\u6570\u7ec4\u5143\u7d20\u4e0e\u5f7c\u6b64\u6bd4\u8f83 \u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u79fb\u5230\u8f83\u5927\u7684\u5143\u7d20\u524d\u9762 2. \u6392\u5e8f\u539f\u59cb\u6570\u7ec4 Arrays.sort() \u65b9\u6cd5\u4e0d\u4f1a\u521b\u5efa\u6570\u7ec4\u7684\u65b0\u526f\u672c\uff0c\u800c\u662f\u5bf9\u5176\u539f\u59cb\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u6570\u7ec4\u88ab\u6392\u5e8f\uff0c\u4efb\u4f55\u5bf9\u5176\u5143\u7d20\u8fdb\u884c\u7684\u4fee\u6539\u90fd\u5c06\u53cd\u6620\u5728\u539f\u59cb\u6570\u7ec4\u4e2d\u3002 3. \u6bd4\u8f83\u5668\u63a5\u53e3 \u5bf9\u4e8e\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 Comparator \u63a5\u53e3\u6765\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002Comparator \u63d0\u4f9b\u4e00\u4e2a compare() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff1a \u8d1f\u6570\u8868\u793a\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u6392\u5728\u524d \u6b63\u6570\u8868\u793a\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u5e94\u8be5\u6392\u5728\u524d \u96f6\u8868\u793a\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49 \u793a\u4f8b\u4ee3\u7801\uff1a \/\/ \u539f\u59cb\u6570\u7ec4 int[] numbers = {5, 2, 8, 1, 4}; \/\/ \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f [&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-37360","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37360","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=37360"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37360\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}