{"id":65419,"date":"2025-05-03T10:00:42","date_gmt":"2025-05-03T02:00:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65419\/"},"modified":"2025-05-03T10:00:42","modified_gmt":"2025-05-03T02:00:42","slug":"java%e5%ad%97%e7%ac%a6-%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65419\/","title":{"rendered":"java\u5b57\u7b26 \u6570\u7ec4\u600e\u4e48\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7 arrays.sort() \u65b9\u6cd5\u8fdb\u884c\u5347\u5e8f\u6392\u5217\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 collections.sort()\u3001stream.sorted() \u7b49\u65b9\u6cd5\u4ee5\u53ca\u81ea\u5b9a\u4e49 comparator \u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111300304031077.jpg\" class=\"aligncenter\" title=\"java\u5b57\u7b26 \u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u5b57\u7b26 \u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>Java \u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u6570\u7ec4\u6309\u7167\u5347\u5e8f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>char[] charArray = {'a', 'b', 'c', 'd', 'e'};\n\nArrays.sort(charArray);\n\nSystem.out.println(Arrays.toString(charArray));  \/\/ \u8f93\u51fa\uff1a[a, b, c, d, e]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6392\u5e8f\u673a\u5236\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u5bf9\u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8be5\u7b97\u6cd5\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\u5206\u522b\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u7684\u90e8\u5206\u3002<\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong><\/p>\n<p>\u5f52\u5e76\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<p><strong>\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86 Arrays.sort() \u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<ul>\n<li> <strong>\u624b\u52a8\u6392\u5e8f\uff1a<\/strong>\u4f7f\u7528\u5faa\u73af\u548c\u6bd4\u8f83\u8bed\u53e5\u624b\u52a8\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\u3002<\/li>\n<li> <strong>Collections.sort()\uff1a<\/strong>\u5c06\u5b57\u7b26\u6570\u7ec4\u5305\u88c5\u5230 List \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 Collections.sort() \u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528 Timsort \u7b97\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li> <strong>Stream.sorted()\uff1a<\/strong>\u4f7f\u7528 Stream API \u4e2d\u7684 sorted() \u65b9\u6cd5\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u6392\u5e8f\u7684 Stream\uff0c\u7136\u540e\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>\u9700\u8981\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b57\u7b26\u6570\u7ec4\u6392\u5e8f\u540e\uff0c\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c06\u88ab\u8986\u76d6\u3002<\/li>\n<li>\u5982\u679c\u5b57\u7b26\u6570\u7ec4\u5305\u542b Unicode \u5b57\u7b26\uff0c\u6392\u5e8f\u987a\u5e8f\u5c06\u53d6\u51b3\u4e8e\u6240\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u548c\u672c\u5730\u5316\u8bbe\u7f6e\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a Comparator \u63a5\u53e3\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 Arrays.sort() \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5b57\u7b26 \u6570\u7ec4\u600e\u4e48\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\u7684\u5b57\u7b26\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7 arrays.sort() \u65b9\u6cd5\u8fdb\u884c\u5347\u5e8f\u6392\u5217\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a o(n log n)\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 collections.sort()\u3001stream.sorted() \u7b49\u65b9\u6cd5\u4ee5\u53ca\u81ea\u5b9a\u4e49 comparator \u6392\u5e8f\u3002 \u5982\u4f55\u5bf9 Java \u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f Java \u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u6570\u7ec4\u6309\u7167\u5347\u5e8f\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a char[] charArray = {&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;e&#8217;}; Arrays.sort(charArray); System.out.println(Arrays.toString(charArray)); \/\/ \u8f93\u51fa\uff1a[a, b, c, d, e] \u767b\u5f55\u540e\u590d\u5236 \u6392\u5e8f\u673a\u5236\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Arrays.sort() \u65b9\u6cd5\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\u5bf9\u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8be5\u7b97\u6cd5\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\u5206\u522b\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u7684\u90e8\u5206\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u5f52\u5e76\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002 \u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5\uff1a \u9664\u4e86 Arrays.sort() \u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a \u624b\u52a8\u6392\u5e8f\uff1a\u4f7f\u7528\u5faa\u73af\u548c\u6bd4\u8f83\u8bed\u53e5\u624b\u52a8\u6bd4\u8f83\u548c\u4ea4\u6362\u5143\u7d20\u3002 Collections.sort()\uff1a\u5c06\u5b57\u7b26\u6570\u7ec4\u5305\u88c5\u5230 List \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 [&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-65419","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65419","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=65419"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65419\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}