{"id":35857,"date":"2024-11-26T15:59:18","date_gmt":"2024-11-26T07:59:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35857\/"},"modified":"2024-11-26T15:59:18","modified_gmt":"2024-11-26T07:59:18","slug":"java-%e4%b8%ad%e7%9a%84%e6%af%94%e8%be%83%e5%99%a8%e6%98%af%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35857\/","title":{"rendered":"Java \u4e2d\u7684\u6bd4\u8f83\u5668\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077942470124.jpg\" class=\"aligncenter\" title=\"Java \u4e2d\u7684\u6bd4\u8f83\u5668\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u63d2\u56fe\" alt=\"Java \u4e2d\u7684\u6bd4\u8f83\u5668\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u63d2\u56fe\" \/><\/p>\n<h2> \u4ecb\u7ecd <\/h2>\n<p>\u6709\u65f6\u5728\u505a\u9879\u76ee\u65f6\u9700\u8981\u5bf9\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u4e3a\u6b64\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u6709\u5fc5\u8981\u5b9e\u73b0\u6211\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f46\u8fd9\u6709\u70b9\u4e0d\u5fc5\u8981\uff0c\u5c3d\u7ba1\u4e86\u89e3\u4e00\u4e0b\u4e5f\u6ca1\u4ec0\u4e48\u574f\u5904\u4ed6\u4eec\u5982\u4f55\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u57fa\u5143\u6570\u7ec4\u5e76\u6309\u5347\u5e8f\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5229\u7528\u65e0\u9700\u5c06\u7ed3\u679c\u5206\u914d\u7ed9\u65b0\u53d8\u91cf\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f\u59cb\u6570\u7ec4\u3002<\/p>\n<pre>int[] numbers = {9, 8, 5, 3, 1, 2, 4, 6, 7};\narrays.sort(numbers);\nsystem.out.println(arrays.tostring(numbers));\n\n\/\/ output\n[1, 2, 3, 4, 5, 6, 7, 8, 9]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u60a8\u6709\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u96c6\u5408\uff08\u4f8b\u5982 movie \u7c7b\u578b\u7684\u8bb0\u5f55\uff09\u65f6\uff0c\u8fd9\u4e5f\u9002\u7528\uff0c\u4f46\u5982\u679c\u6211\u4eec\u770b\u5230 arrays.sort() \u65b9\u6cd5\uff0c\u5b83\u4e0d\u63a5\u53d7\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u6570\u7ec4\uff0c\u56e0\u6b64\u5b83\u5fc5\u987b\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u63a5\u53d7 t \u7c7b\u578b\u7684\u5bf9\u8c61\u548c comparator \u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\u3002\u8fd9\u4e2a\u63a5\u53e3\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a java \u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u65b9\u6cd5\u90fd\u4f7f\u7528\u5b83\u4ee5\u81ea\u5b9a\u4e49\u65b9\u5f0f\u6bd4\u8f83\u5bf9\u8c61\u3002\u4f8b\u5982\uff0clist \u5bf9\u8c61\u7684 collections.sort() \u65b9\u6cd5\u6216 sort() \u65b9\u6cd5\uff0c\u751a\u81f3 streams \u4e5f\u63a5\u53d7 comparator \u6765\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u6bd4\u8f83\u5668\uff1f <\/h2>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3<strong>comparator<\/strong>\uff08\u51fd\u6570\u5f0f\u5b83\u53ef\u4ee5\u5199\u6210lambda\u8868\u8fbe\u5f0f\uff09\u662f\u4e00\u4e2a\u5141\u8bb8\u4f60\u6bd4\u8f83\u4e24\u4e2at\u7c7b\u578b\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u5b83\u7528\u4e8e\u6bd4\u8f83\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u81ea\u5b9a\u4e49\u7269\u4f53\u7b49\u8be5\u63a5\u53e3\u6709\u51e0\u4e2a\u9759\u6001\u548c\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u4f46\u91cd\u8981\u7684\u662fcompare() \u65b9\u6cd5\uff0c\u5b83\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002 compare() \u63a5\u6536\u4e24\u4e2a t \u7c7b\u578b\u7684\u5bf9\u8c61\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\u65b9\u6cd5\u7b7e\u540d\u5982\u4e0b\uff1a<\/p>\n<pre>int compare(t o1, t o2);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679co1\u5c0f\u4e8eo2\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u8d1f\u6570\uff1b\u5982\u679c\u76f8\u7b49\uff0c\u5219\u8fd4\u56de\u96f6\uff1b\u5982\u679co1\u5927\u4e8eo2\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u6b63\u6570\uff0c\u901a\u5e38\u5206\u522b\u8fd4\u56de-1\u30010\u62161\u3002 <\/p>\n<h3> \u4e00\u4e2a\u5bf9\u8c61\u5c0f\u4e8e\u3001\u7b49\u4e8e\u6216\u5927\u4e8e\u53e6\u4e00\u4e2a\u5bf9\u8c61\u610f\u5473\u7740\u4ec0\u4e48\uff1f <\/h3>\n<p>\u8ba9\u6211\u4eec\u5206\u6790\u4e00\u4e0bcompare()\u65b9\u6cd5\u8fd4\u56de\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a\u5bf9\u8c61\u7684\u6392\u5e8f\u53d6\u51b3\u4e8e\u6b64\uff0c\u91cd\u8981\u7684\u662f\u8981\u8003\u8651\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684\u542b\u4e49\u662f\u76f8\u5bf9\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u60a8\u60f3\u8981\u5347\u5e8f\u6216\u964d\u5e8f\u3002\u8fd9\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u4ee5\u53ca\u5982\u4f55\u5b9e\u65bd\u3002\u8ba9\u6211\u4eec\u4e3a\u6bcf\u4e2a\u793a\u4f8b\u8003\u8651\u4ee5\u4e0b<em>\u8bb0\u5f55<\/em>\uff1a<\/p>\n<pre>public record movie(\n        string name,\n        list&lt;string&gt; actors,\n        int budget,\n        int year\n) {\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c0f\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5219\u8fd4\u56de\u8d1f\u6570\u3002\u4f8b\u5982\uff0c\u8981\u6309\u53d1\u884c\u5e74\u4efd\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u5f53\u7535\u5f71 a \u5c0f\u4e8e\u7535\u5f71 b \u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de -1\uff1a <\/li>\n<\/ul>\n<pre>\/\/ a &lt; b -&gt; -1\na.year() &lt; b.year() -&gt; -1\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5927\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5219\u8fd4\u56de\u6b63\u6570\u3002\u4f8b\u5982\uff0c\u8981\u6309\u9884\u7b97\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u5f53\u7535\u5f71 a \u5927\u4e8e\u7535\u5f71 b \u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de 1\uff1a <\/li>\n<\/ul>\n<pre>\/\/ a &gt; b -&gt; 1\na.budget() &gt; b.budget() -&gt; 1\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u7b49\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5219\u8fd4\u56de\u96f6\u3002\u4f8b\u5982\uff0c\u8981\u6309\u6f14\u5458\u6570\u91cf\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u5f53\u7535\u5f71 a \u7b49\u4e8e\u7535\u5f71 b \u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de 0\uff1a <\/li>\n<\/ul>\n<pre>\/\/ a == b -&gt; 0\na.actors().size() == b.actors().size() -&gt; 0\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u4f7f\u7528\u6bd4\u8f83\u5668 <\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u5728 list \u7c7b\u578b\u7684\u5bf9\u8c61\u4e2d\u6709\u4ee5\u4e0b\u7535\u5f71\uff1a  <\/p>\n<pre>movie movie1 = new movie(\"the godfather\", arrays.aslist(\"marlon brando\", \"al pacino\"), 6000000, 1972);\nmovie movie2 = new movie(\"the godfather: part ii\", arrays.aslist(\"al pacino\", \"robert de niro\"), 13000000, 1974);\nmovie movie3 = new movie(\"the shawshank redemption\", arrays.aslist(\"tim robbins\", \"morgan freeman\"), 25000000, 1994);\nmovie movie4 = new movie(\"the dark knight\", arrays.aslist(\"christian bale\", \"heath ledger\"), 185000000, 2008);\n\nlist&lt;movie&gt; movies = arrays.aslist(movie1, movie2, movie3, movie4);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6309\u4e0a\u6620\u5e74\u4efd\u5347\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2acomparator \u7c7b\u578b\u7684\u5bf9\u8c61\u5e76\u91cd\u5199compare()\u65b9\u6cd5\uff0c\u7136\u540e\u5c06\u6b64\u5bf9\u8c61\u4f20\u9012\u7ed9\u5217\u8868\u7684sort()\u65b9\u6cd5\uff1a  <\/p>\n<pre>comparator&lt;movie&gt; comparatorbyyear = new comparator&lt;movie&gt;() {\n    @override\n    public int compare(movie o1, movie o2) {\n        return o1.year() - o2.year();\n    }\n};\n\nmovies.sort(comparatorbyyear);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b83\u4e5f\u53ef\u4ee5\u5728 sort() \u65b9\u6cd5\u4e2d\u5b9e\u73b0\u4e3a\u533f\u540d\u7c7b\uff1a<\/p>\n<pre>movies.sort(new comparator&lt;movie&gt;() {\n    @override\n    public int compare(movie o1, movie o2) {\n        return o1.year() - o2.year();\n    }\n});\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6216\u8005\u66f4\u7b80\u6d01\u5730\u76f4\u63a5\u5728 sort() \u65b9\u6cd5\u4e2d\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\uff1a<\/p>\n<pre>movies.sort((p1, p2) -&gt; p1.year() - p2.year());\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4efb\u4f55\u8fd9\u4e9b\u5b9e\u73b0\u200b\u200b\u90fd\u4f1a\u6309\u53d1\u5e03\u5e74\u4efd\u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u5982\u679c\u8981\u6309\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u66f4\u6539 lambda \u8868\u8fbe\u5f0f\u4e2d\u53c2\u6570\u7684\u987a\u5e8f\uff0c\u6216\u8005\u5728\u51cf\u6cd5\u4e2d\u6dfb\u52a0\u8d1f\u53f7\uff1a<\/p>\n<pre>movies.sort((p1, p2) -&gt; p2.year() - p1.year());\n\/\/ o\nmovies.sort((p1, p2) -&gt; - (p1.year() - p2.year()));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u4f55\u5bf9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684\u4e00\u4e9b\u5176\u4ed6\u793a\u4f8b\u662f\uff1a<\/p>\n<ul>\n<li>\u6309\u6f14\u5458\u6570\u91cf\u5347\u5e8f\u6392\u5217\u7535\u5f71\uff08\u4ece\u5c11\u5230\u591a\uff09\uff1a <\/li>\n<\/ul>\n<pre>movies.sort((p1, p2) -&gt; p1.actors().size() - p2.actors().size());\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u6309\u9884\u7b97\u964d\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff08\u4ece\u6700\u9ad8\u9884\u7b97\u5230\u6700\u4f4e\u9884\u7b97\uff09\uff1a <\/li>\n<\/ul>\n<pre>movies.sort((p1, p2) -&gt; p2.budget() - p1.budget());\n\/\/ o \nmovies.sort((p1, p2) -&gt; - (p1.budget() - p2.budget()));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u6309\u540d\u79f0\u5347\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff1a <\/li>\n<\/ul>\n<pre>movies.sort((p1, p2) -&gt; p1.name().compareto(p2.name()));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u5176\u4ed6\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u6309\u964d\u5e8f\u5bf9\u6574\u6570\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684\u60c5\u51b5\uff0c<\/p>\n<pre>list&lt;integer&gt; numbers = arrays.aslist(5, 3, 1, 2, 4);\nnumbers.sort((n1, n2) -&gt; n2 - n1);\n\n\/\/ output\n[5, 4, 3, 2, 1]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e3a\u6b64\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u65b9\u6cd5 comparator.reverseorder() \u8fd4\u56de\u4e00\u4e2a\u6309\u964d\u5e8f\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u7684\u6bd4\u8f83\u5668\uff0c\u4ee5\u53ca\u4f7f\u7528 comparator.naturalorder() \u6309\u5347\u5e8f\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>numbers.sort(comparator.reverseorder());\nnumbers.sort(comparator.naturalorder());\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u4f7f\u7528 integer.compare() <\/h3>\n<p>\u5728 java \u4e2d\u5df2\u7ecf\u6709\u4e00\u4e9b\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u4ee5\u6709\u6548\u7684\u65b9\u5f0f\u6267\u884c\u8fd9\u79cd\u7c7b\u578b\u7684\u6bd4\u8f83\uff0c\u4f8b\u5982 integer.compare() \uff0c\u5b83\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c0f\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u96f6\uff0c\u5219\u8fd4\u56de\u8d1f\u6570\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5927\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5219\u76f8\u7b49\u4e14\u4e3a\u6b63\u6570\u3002\u5982\u679c\u6211\u4eec\u5206\u6790\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u4e0e\u4e0a\u9762\u89e3\u91ca\u7684\u7c7b\u4f3c\uff0c\u5e76\u4e14\u8fd4\u56de\u7684\u6b63\u662fcomparator\u63a5\u53e3\u7684compare()\u65b9\u6cd5\u6240\u9700\u8981\u7684\u3002 integer.compare() \u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>public static int compare(int x, int y) {\n    return (x &lt; y) ? -1 : ((x == y) ? 0 : 1);\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u60f3\u6309\u53d1\u884c\u5e74\u4efd\u5347\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 integer.compare():<\/p>\n<pre>movies.sort((p1, p2) -&gt; integer.compare(p1.year(), p2.year()));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u4f7f\u7528\u53c2\u8003\u65b9\u6cd5 <\/h3>\n<p>\u6709\u65f6\u53ef\u4ee5\u4f7f\u7528\u5f15\u7528\u65b9\u6cd5\u6765\u6267\u884c\u4e0e\u4ee5\u524d\u4e0d\u540c\u7684\u6bd4\u8f83\uff0c\u4f8b\u5982\uff0c\u6309\u5347\u5e8f\u5bf9\u6574\u6570\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>list&lt;integer&gt; numbers = arrays.aslist(5, 3, 1, 2, 4);\nnumbers.sort(integer::compareto);\n\n\/\/ output\n[1, 2, 3, 4, 5]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>integer \u4e0d\u662f\u552f\u4e00\u5177\u6709compareto() \u65b9\u6cd5\u7684\u7c7b\uff0c\u4f8b\u5982 string \u6709\u4e00\u4e2acompareto() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u5b57\u5178\u987a\u5e8f\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u751a\u81f3\u53ef\u4ee5\u5c06 charsequence \u4e0e\u5176\u4e00\u8d77\u4f7f\u7528compare() \u65b9\u6cd5\uff08\u6280\u672f\u4e0a\u8868\u793a\u5b57\u7b26\u5e8f\u5217\uff09\u3002<\/p>\n<pre>list&lt;string&gt; names = arrays.aslist(\"john\", \"alice\", \"bob\", \"charlie\");\nnames.sort(charsequence::compare);\nnames.sort(string::compareto);\n\n\/\/ output\n[alice, bob, charlie, john]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56de\u5230\u7535\u5f71\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u60f3\u6309\u7167\u4e0a\u6620\u5e74\u4efd\u5347\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528compareingint()\u65b9\u6cd5\u4f5c\u4e3a\u53c2\u8003\u65b9\u6cd5\uff1a<\/p>\n<pre>movies.sort(comparator.comparingint(movie::year));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6216\u8005\u6839\u636e string \u7c7b\u578b\u5c5e\u6027\u8fdb\u884c\u6bd4\u8f83\uff0c\u5728\u672c\u4f8b\u4e2d\u4e3a\u7535\u5f71\u540d\u79f0\uff1a<\/p>\n<pre>movies.sort(comparator.comparing(movie::name));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u6309\u591a\u4e2a\u5c5e\u6027\u6392\u5e8f <\/h3>\n<p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6309\u591a\u4e2a\u5c5e\u6027\u5bf9\u5bf9\u8c61\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6309\u53d1\u884c\u5e74\u4efd\u5347\u5e8f\u5bf9\u7535\u5f71\u8fdb\u884c\u6392\u5e8f\uff0c\u6309\u9884\u7b97\u964d\u5e8f\u6392\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 thencomparing() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u6bd4\u8f83\u5668\uff0c\u8d1f\u8d23\u6309\u591a\u4e2a\u5c5e\u6027\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e24\u90e8\u7535\u5f71a\u548cb\uff0c\u4e0a\u6620\u5e74\u4efd\u76f8\u540c\uff0c\u5219\u6309\u9884\u7b97\u6392\u5e8f\u3002<\/p>\n<pre>movies.sort(Comparator.comparingInt(Movie::year).thenComparing((p1, p2) -&gt; p2.budget() - p1.budget()));\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u6bd4\u8f83\u5668\u5728java\u4e2d\u5f88\u591a\u65f6\u5019\u90fd\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u8ba9\u4f60\u4ee5\u4e2a\u6027\u5316\u7684\u65b9\u5f0f\u6bd4\u8f83\u5bf9\u8c61\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u5b83\u4eec\u8fd8\u53ef\u4ee5\u7528\u5728\u8bb8\u591ajava\u96c6\u5408\u65b9\u6cd5\u4e2d\uff0c\u751a\u81f3\u53ef\u4ee5\u6709\u591a\u4e2a\u6bd4\u8f83\u5668\u6765\u6392\u5e8f\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u60a8\u90fd\u53ef\u4ee5\u67e5\u9605 ide \u4e2d\u7684 comparator \u6587\u6863\u6216\u5b98\u65b9 java \u6587\u6863\uff0c\u4e86\u89e3\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u5b83\u4eec\u3002 <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u4e2d\u7684\u6bd4\u8f83\u5668\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\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>\u4ecb\u7ecd \u6709\u65f6\u5728\u505a\u9879\u76ee\u65f6\u9700\u8981\u5bf9\u67d0\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u4e3a\u6b64\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u6709\u5fc5\u8981\u5b9e\u73b0\u6211\u4eec\u81ea\u5df1\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f46\u8fd9\u6709\u70b9\u4e0d\u5fc5\u8981\uff0c\u5c3d\u7ba1\u4e86\u89e3\u4e00\u4e0b\u4e5f\u6ca1\u4ec0\u4e48\u574f\u5904\u4ed6\u4eec\u5982\u4f55\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u57fa\u5143\u6570\u7ec4\u5e76\u6309\u5347\u5e8f\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5229\u7528\u65e0\u9700\u5c06\u7ed3\u679c\u5206\u914d\u7ed9\u65b0\u53d8\u91cf\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4fee\u6539\u4e86\u539f\u59cb\u6570\u7ec4\u3002 int[] numbers = {9, 8, 5, 3, 1, 2, 4, 6, 7}; arrays.sort(numbers); system.out.println(arrays.tostring(numbers)); \/\/ output [1, 2, 3, 4, 5, 6, 7, 8, 9] \u767b\u5f55\u540e\u590d\u5236 \u5f53\u60a8\u6709\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u96c6\u5408\uff08\u4f8b\u5982 movie \u7c7b\u578b\u7684\u8bb0\u5f55\uff09\u65f6\uff0c\u8fd9\u4e5f\u9002\u7528\uff0c\u4f46\u5982\u679c\u6211\u4eec\u770b\u5230 arrays.sort() \u65b9\u6cd5\uff0c\u5b83\u4e0d\u63a5\u53d7\u8fd9\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u6570\u7ec4\uff0c\u56e0\u6b64\u5b83\u5fc5\u987b\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u63a5\u53d7 t \u7c7b\u578b\u7684\u5bf9\u8c61\u548c comparator \u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\u3002\u8fd9\u4e2a\u63a5\u53e3\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a java \u4e2d\u7684\u8bb8\u591a\u5176\u4ed6\u65b9\u6cd5\u90fd\u4f7f\u7528\u5b83\u4ee5\u81ea\u5b9a\u4e49\u65b9\u5f0f\u6bd4\u8f83\u5bf9\u8c61\u3002\u4f8b\u5982\uff0clist \u5bf9\u8c61\u7684 collections.sort() \u65b9\u6cd5\u6216 sort() \u65b9\u6cd5\uff0c\u751a\u81f3 streams \u4e5f\u63a5\u53d7 comparator \u6765\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002 \u4ec0\u4e48\u662f\u6bd4\u8f83\u5668\uff1f \u51fd\u6570\u5f0f\u63a5\u53e3comparator\uff08\u51fd\u6570\u5f0f\u5b83\u53ef\u4ee5\u5199\u6210lambda\u8868\u8fbe\u5f0f\uff09\u662f\u4e00\u4e2a\u5141\u8bb8\u4f60\u6bd4\u8f83\u4e24\u4e2at\u7c7b\u578b\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u56e0\u6b64\u5b83\u7528\u4e8e\u6bd4\u8f83\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u81ea\u5b9a\u4e49\u7269\u4f53\u7b49\u8be5\u63a5\u53e3\u6709\u51e0\u4e2a\u9759\u6001\u548c\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u4f46\u91cd\u8981\u7684\u662fcompare() \u65b9\u6cd5\uff0c\u5b83\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002 compare() \u63a5\u6536\u4e24\u4e2a [&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-35857","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35857","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=35857"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35857\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}