{"id":1179039,"date":"2025-01-15T18:18:21","date_gmt":"2025-01-15T10:18:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179039.html"},"modified":"2025-01-15T18:18:24","modified_gmt":"2025-01-15T10:18:24","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89sort%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179039.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u5b9a\u4e49sort\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113552\/42a63ef2-1bd7-4cb7-9868-c260f0326335.webp\" alt=\"Python\u4e2d\u5982\u4f55\u5b9a\u4e49sort\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5b9a\u4e49sort\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u5185\u7f6e\u7684sorted\u51fd\u6570\u3001\u6216\u901a\u8fc7\u7c7b\u65b9\u6cd5\u5b9a\u4e49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u662f\u4e3a\u4e86\u6309\u7279\u5b9a\u89c4\u5219\u6392\u5e8f\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u89c4\u5219\u662f\u57fa\u4e8e\u5143\u7d20\u7684\u67d0\u4e2a\u5c5e\u6027\u6216\u5143\u7d20\u95f4\u7684\u6bd4\u8f83\u903b\u8f91\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(item):<\/p>\n<p>    return item.attribute<\/p>\n<p>data_list = [Object1, Object2, Object3]<\/p>\n<p>sorted_list = sorted(data_list, key=custom_sort)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>custom_sort<\/code> \u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u5217\u8868\u9879\u5e76\u8fd4\u56de\u8be5\u9879\u7684\u67d0\u4e2a\u5c5e\u6027\uff0c\u7528\u4e8e\u6392\u5e8f\u3002<code>sorted<\/code> \u51fd\u6570\u4f7f\u7528 <code>key<\/code> \u53c2\u6570\u8c03\u7528 <code>custom_sort<\/code> \u6765\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SORT \u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c<code>sort<\/code> \u51fd\u6570\u662f\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5bf9\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002<code>sort<\/code> \u51fd\u6570\u6709\u4e24\u4e2a\u4e3b\u8981\u65b9\u6cd5\uff1a\u4e00\u79cd\u662f\u76f4\u63a5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684 <code>list.sort()<\/code> \u65b9\u6cd5\uff0c\u53e6\u4e00\u79cd\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u7684 <code>sorted()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.1 list.sort() \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>list.sort()<\/code> \u65b9\u6cd5\u76f4\u63a5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e14\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002\u5b83\u4e0d\u4f1a\u8fd4\u56de\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u5c31\u5730\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 sorted() \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code> \u51fd\u6570\u5219\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u4e0d\u4f1a\u4fee\u6539\u539f\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u5e94\u7528\u4e8e\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [5, 2, 9, 1, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u662f\u6307\u7528\u6237\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\u4f20\u9012\u7ed9 <code>sort<\/code> \u6216 <code>sorted<\/code> \u51fd\u6570\uff0c\u4ece\u800c\u6839\u636e\u7279\u5b9a\u7684\u903b\u8f91\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4f7f\u7528 <code>key<\/code> \u53c2\u6570\u5c06\u5176\u4f20\u9012\u7ed9 <code>sort<\/code> \u6216 <code>sorted<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>def custom_sort(x):<\/p>\n<p>    return x % 10<\/p>\n<p>numbers = [15, 23, 9, 42, 55, 1]<\/p>\n<p>sorted_numbers = sorted(numbers, key=custom_sort)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 42, 23, 15, 55, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570 <code>custom_sort<\/code> \u8fd4\u56de\u6bcf\u4e2a\u6570\u5b57\u7684\u4e2a\u4f4d\u6570\uff0c\u5e76\u6839\u636e\u8fd9\u4e2a\u503c\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002lambda \u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>numbers = [15, 23, 9, 42, 55, 1]<\/p>\n<p>sorted_numbers = sorted(numbers, key=lambda x: x % 10)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 42, 23, 15, 55, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u6765\u5b9e\u73b0\u591a\u6761\u4ef6\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>students = [<\/p>\n<p>    (&#39;Alice&#39;, 25, &#39;A&#39;),<\/p>\n<p>    (&#39;Bob&#39;, 22, &#39;B&#39;),<\/p>\n<p>    (&#39;Charlie&#39;, 25, &#39;B&#39;),<\/p>\n<p>    (&#39;David&#39;, 22, &#39;A&#39;)<\/p>\n<p>]<\/p>\n<p>sorted_students = sorted(students, key=lambda x: (x[1], x[2]))<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [(&#39;Bob&#39;, 22, &#39;B&#39;), (&#39;David&#39;, 22, &#39;A&#39;), (&#39;Charlie&#39;, 25, &#39;B&#39;), (&#39;Alice&#39;, 25, &#39;A&#39;)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b66\u751f\u5217\u8868\u9996\u5148\u6309\u5e74\u9f84\u6392\u5e8f\uff0c\u7136\u540e\u6309\u6210\u7ee9\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6392\u5e8f\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>sort<\/code> \u548c <code>sorted<\/code> \u51fd\u6570\u662f\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd9\u610f\u5473\u7740\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49\uff0c\u5b83\u4eec\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u4f1a\u6539\u53d8\u3002\u8fd9\u5bf9\u4e8e\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>students = [<\/p>\n<p>    (&#39;Alice&#39;, 25, &#39;A&#39;),<\/p>\n<p>    (&#39;Bob&#39;, 25, &#39;B&#39;),<\/p>\n<p>    (&#39;Charlie&#39;, 25, &#39;A&#39;),<\/p>\n<p>    (&#39;David&#39;, 25, &#39;B&#39;)<\/p>\n<p>]<\/p>\n<p>sorted_students = sorted(students, key=lambda x: x[2])<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [(&#39;Alice&#39;, 25, &#39;A&#39;), (&#39;Charlie&#39;, 25, &#39;A&#39;), (&#39;Bob&#39;, 25, &#39;B&#39;), (&#39;David&#39;, 25, &#39;B&#39;)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b66\u751f\u9996\u5148\u6309\u6210\u7ee9\u6392\u5e8f\uff0c\u6210\u7ee9\u76f8\u540c\u7684\u5b66\u751f\u4fdd\u6301\u4e86\u539f\u6765\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u53cd\u5411\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>sort<\/code> \u548c <code>sorted<\/code> \u51fd\u6570\u90fd\u652f\u6301 <code>reverse<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6309\u5bf9\u8c61\u5c5e\u6027\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u6839\u636e\u5bf9\u8c61\u7684\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u9700\u8981\u5728\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u4e2d\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>class Student:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>students = [Student(&#39;Alice&#39;, 25), Student(&#39;Bob&#39;, 22), Student(&#39;Charlie&#39;, 23)]<\/p>\n<p>sorted_students = sorted(students, key=lambda s: s.age)<\/p>\n<p>for student in sorted_students:<\/p>\n<p>    print(student.name, student.age)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Bob 22<\/strong><\/h2>\n<h2><strong>Charlie 23<\/strong><\/h2>\n<h2><strong>Alice 25<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u590d\u6742\u6392\u5e8f\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7ed3\u5408\u591a\u4e2a\u6392\u5e8f\u6761\u4ef6\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u548c lambda \u8868\u8fbe\u5f0f\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>class Student:<\/p>\n<p>    def __init__(self, name, age, grade):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.grade = grade<\/p>\n<p>students = [<\/p>\n<p>    Student(&#39;Alice&#39;, 25, &#39;A&#39;),<\/p>\n<p>    Student(&#39;Bob&#39;, 22, &#39;B&#39;),<\/p>\n<p>    Student(&#39;Charlie&#39;, 25, &#39;B&#39;),<\/p>\n<p>    Student(&#39;David&#39;, 22, &#39;A&#39;)<\/p>\n<p>]<\/p>\n<h2><strong>\u6309\u5e74\u9f84\u3001\u6210\u7ee9\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=lambda s: (s.age, s.grade))<\/p>\n<p>for student in sorted_students:<\/p>\n<p>    print(student.name, student.age, student.grade)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>David 22 A<\/strong><\/h2>\n<h2><strong>Bob 22 B<\/strong><\/h2>\n<h2><strong>Alice 25 A<\/strong><\/h2>\n<h2><strong>Charlie 25 B<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b66\u751f\u9996\u5148\u6309\u5e74\u9f84\u6392\u5e8f\uff0c\u7136\u540e\u6309\u6210\u7ee9\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3001lambda \u8868\u8fbe\u5f0f\u3001\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f\u3001\u53cd\u5411\u6392\u5e8f\u548c\u6839\u636e\u5bf9\u8c61\u5c5e\u6027\u6392\u5e8f\uff0cPython \u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6392\u5e8f\u529f\u80fd\u3002<strong>\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u9ad8\u6548\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong> \u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u89c4\u5219\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f<\/strong> \u53ef\u4ee5\u7b80\u5316\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u3002<strong>\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f<\/strong> \u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u6765\u5b9e\u73b0\uff0c<strong>\u6392\u5e8f\u7684\u7a33\u5b9a\u6027<\/strong> \u786e\u4fdd\u4e86\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d8\u3002<strong>\u53cd\u5411\u6392\u5e8f<\/strong> \u53ef\u4ee5\u901a\u8fc7 <code>reverse<\/code> \u53c2\u6570\u5b9e\u73b0\uff0c<strong>\u6309\u5bf9\u8c61\u5c5e\u6027\u6392\u5e8f<\/strong> \u5219\u9700\u8981\u5728\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u4e2d\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6392\u5e8f\u529f\u80fd\u548c\u7075\u6d3b\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u65b9\u6cd5\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0csort\u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>sort\u51fd\u6570\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u5b83\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\u7684\u987a\u5e8f\u3002\u901a\u8fc7sort\u51fd\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u6309\u7167\u9ed8\u8ba4\u7684\u5347\u5e8f\u6216\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u503c\u548c\u5b57\u7b26\u4e32\u7b49\u53ef\u6bd4\u8f83\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528sort\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0csort\u51fd\u6570\u5141\u8bb8\u7528\u6237\u901a\u8fc7key\u53c2\u6570\u6765\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u8c03\u7528\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u6216\u67d0\u4e9b\u7279\u5b9a\u7684\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<p><strong>sort\u51fd\u6570\u4e0esorted\u51fd\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>sort\u51fd\u6570\u662f\u5217\u8868\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5b83\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u800csorted\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u59cb\u6570\u636e\u4e0d\u53d8\uff0c\u4f7f\u7528sorted\u51fd\u6570\u4f1a\u66f4\u5408\u9002\u3002\u540c\u65f6\uff0csorted\u51fd\u6570\u4e5f\u53ef\u4ee5\u63a5\u6536key\u548creverse\u53c2\u6570\u8fdb\u884c\u6392\u5e8f\u63a7\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5b9a\u4e49sort\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u5185\u7f6e\u7684sorted\u51fd\u6570\u3001\u6216\u901a [&hellip;]","protected":false},"author":3,"featured_media":1179049,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179039"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1179039"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179039\/revisions"}],"predecessor-version":[{"id":1179053,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179039\/revisions\/1179053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179049"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}