{"id":75,"date":"2025-06-03T11:00:37","date_gmt":"2025-06-05T03:12:37","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=75"},"modified":"2025-06-05T11:00:37","modified_gmt":"2025-06-05T03:12:37","slug":"swift-dictionaryarray-%e7%af%84%e4%be%8b-sort-%e6%8e%92%e5%ba%8f-%e5%be%9e%e5%a4%a7%e5%88%b0%e5%b0%8f-%e5%be%9e%e5%b0%8f%e5%88%b0%e5%a4%a7-key-%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-dictionaryarray-%e7%af%84%e4%be%8b-sort-%e6%8e%92%e5%ba%8f-%e5%be%9e%e5%a4%a7%e5%88%b0%e5%b0%8f-%e5%be%9e%e5%b0%8f%e5%88%b0%e5%a4%a7-key-%e6%8e%92%e5%ba%8f\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a2025 \u6700\u65b0\u5b57\u5178\u8207\u9663\u5217\u6392\u5e8f\u65b9\u6cd5"},"content":{"rendered":"<h1>Swift \u7a0b\u5f0f\u6559\u5b78\uff1a2025 \u6700\u65b0\u5b57\u5178\u8207\u9663\u5217\u6392\u5e8f\u65b9\u6cd5<\/h1>\n<h2>\u6392\u5e8f\u4ecb\u7d39 Sorting Algorithm<\/h2>\n<p>\u5728\u8a08\u7b97\u6a5f\u79d1\u5b78\u4e2d\uff0c\u6392\u5e8f\uff08Sorting Algorithm\uff09\u662f\u4e00\u7a2e\u5c07\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6309\u7279\u5b9a\u9806\u5e8f\u653e\u7f6e\u7684\u7b97\u6cd5\u3002\u6700\u5e38\u7528\u7684\u6392\u5e8f\u65b9\u5f0f\u662f\u6578\u5b57\u9806\u5e8f\u548c\u5b57\u5178\u9806\u5e8f\u3002\u9ad8\u6548\u7684\u6392\u5e8f\u5c0d\u65bc\u512a\u5316\u5176\u4ed6\u9700\u8981\u8f38\u5165\u6578\u64da\u4f4d\u65bc\u6392\u5e8f\u5217\u8868\u4e2d\u7684\u7b97\u6cd5\uff08\u4f8b\u5982\u641c\u5c0b\u548c\u5408\u4f75\u7b97\u6cd5\uff09\u7684\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u6392\u5e8f\u901a\u5e38\u9084\u7528\u65bc\u898f\u7bc4\u5316\u6578\u64da\u548c\u7522\u751f\u4eba\u985e\u53ef\u8b80\u7684\u8f38\u51fa\u3002\u4efb\u4f55\u6392\u5e8f\u7b97\u6cd5\u7684\u8f38\u51fa\u5fc5\u9808\u6eff\u8db3\u5169\u500b\u689d\u4ef6\uff1a<\/p>\n<p>1. \u8f38\u51fa\u6309\u4e0d\u964d\u5e8f\u6392\u5217\uff08\u6839\u64da\u6240\u9700\u7684\u7e3d\u9806\u5e8f\uff0c\u6bcf\u500b\u5143\u7d20\u4e0d\u5c0f\u65bc\u524d\u4e00\u500b\u5143\u7d20\uff09\u3002<br \/>\n2. \u8f38\u51fa\u662f\u8f38\u5165\u7684\u6392\u5217\uff08\u91cd\u65b0\u6392\u5e8f\uff0c\u4f46\u4fdd\u7559\u4e86\u6240\u6709\u539f\u59cb\u5143\u7d20\uff09\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u8f38\u5165\u6578\u64da\u901a\u5e38\u5b58\u5132\u5728\u5141\u8a31\u96a8\u6a5f\u8a2a\u554f\u7684\u6578\u7d44\u4e2d\uff0c\u800c\u4e0d\u662f\u50c5\u5141\u8a31\u9806\u5e8f\u8a2a\u554f\u7684\u5217\u8868\u4e2d\u3002\u7d93\u904e\u9069\u7576\u7684\u4fee\u6539\uff0c\u8a31\u591a\u7b97\u6cd5\u90fd\u53ef\u4ee5\u61c9\u7528\u65bc\u9019\u5169\u7a2e\u985e\u578b\u7684\u6578\u64da\u3002<\/p>\n<h2>Swift \u9663\u5217\u6392\u5e8f Sorted<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Swift \u4e2d\u5c0d\u9663\u5217\u9032\u884c\u6392\u5e8f\u7684\u57fa\u672c\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet testList = [&#8220;1&#8221;, &#8220;5&#8221;, &#8220;2&#8221;, &#8220;4&#8221;, &#8220;3&#8221;]\n&#8220;`<\/p>\n<h2>Swift \u9663\u5217\u6392\u5e8f\u5f9e\u5927\u5230\u5c0f<\/h2>\n<p>\u4f7f\u7528 `sorted(by:)` \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u9663\u5217\u5f9e\u5927\u5230\u5c0f\u6392\u5e8f\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet sortedDescending = testList.sorted(by: >)<br \/>\nprint(sortedDescending) \/\/ [&#8220;5&#8221;, &#8220;4&#8221;, &#8220;3&#8221;, &#8220;2&#8221;, &#8220;1&#8221;]\n&#8220;`<\/p>\n<h2>Swift \u9663\u5217\u6392\u5e8f\u5f9e\u5c0f\u5230\u5927<\/h2>\n<p>\u540c\u6a23\uff0c\u4e5f\u53ef\u4ee5\u5c07\u9663\u5217\u5f9e\u5c0f\u5230\u5927\u6392\u5e8f\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet sortedAscending = testList.sorted(by: <)\nprint(sortedAscending) \/\/ [\"1\", \"2\", \"3\", \"4\", \"5\"]\n```\n\n\n\n<h2>Swift \u5b57\u5178\u9375\u6392\u5e8f Sorted Keys<\/h2>\n<p>Swift \u4e5f\u5141\u8a31\u5c0d\u5b57\u5178\u7684\u9375\u9032\u884c\u6392\u5e8f\uff0c\u9019\u88e1\u6709\u5169\u7a2e\u5275\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar companyTypeList = [String: String]()<br \/>\nvar companyTypeList2 = Dictionary<String, String>()<br \/>\n&#8220;`<\/p>\n<h3>\u6392\u5e8f\u5370\u51fa\u9375\u7684\u9806\u5e8f<\/h3>\n<p>\u8981\u6253\u5370\u6392\u5e8f\u5f8c\u7684\u5b57\u5178\u9375\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet sortedKeys = Array(companyTypeList.keys).sorted(by: >)<br \/>\nfor key in sortedKeys {<br \/>\n    print(&#8220;\\(key): \\(companyTypeList[key]!)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u88e1\u7684 `sorted(by: >)` \u6703\u5c07\u9375\u6309\u964d\u5e8f\u6392\u5217\u3002\u76f8\u61c9\u5730\uff0c\u82e5\u8981\u6309\u5347\u5e8f\u6392\u5217\uff0c\u53ea\u9700\u5c07 `>` \u6539\u70ba `<`\u3002\n\n<img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Dictionary\u6392\u5e8f Array\u6392\u5e8f\" title=\"\"><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 Q&#038;A<\/h2>\n<h3>Q1: \u5982\u4f55\u5c0d\u5b57\u5178\u7684\u503c\u9032\u884c\u6392\u5e8f\uff1f<\/h3>\n<p>A: \u53ef\u4ee5\u5148\u5c07\u5b57\u5178\u7684\u9375\u8f49\u63db\u70ba\u6578\u7d44\uff0c\u7136\u5f8c\u6839\u64da\u9375\u4f86\u6392\u5e8f\u5b57\u5178\u7684\u503c\u3002<\/p>\n<h3>Q2: \u9664\u4e86\u57fa\u672c\u7684\u6392\u5e8f\uff0cSwift \u9084\u6709\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u55ce\uff1f<\/h3>\n<p>A: \u662f\u7684\uff0cSwift \u63d0\u4f9b\u4e86\u591a\u7a2e\u6392\u5e8f\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684\u6392\u5e8f\u689d\u4ef6\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6392\u5e8f\u908f\u8f2f\u3002<\/p>\n<h3>Q3: \u5982\u679c\u9047\u5230\u6392\u5e8f\u5931\u6557\u7684\u60c5\u6cc1\uff0c\u61c9\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>A: \u6aa2\u67e5\u50b3\u5165 `sorted(by:)` \u65b9\u6cd5\u7684\u6392\u5e8f\u689d\u4ef6\uff0c\u78ba\u4fdd\u908f\u8f2f\u6b63\u78ba\uff0c\u4e26\u78ba\u8a8d\u6578\u64da\u985e\u578b\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u4e0d\u50c5\u66f4\u65b0\u4e86\u904e\u6642\u7684\u8a9e\u6cd5\uff0c\u9084\u589e\u5f37\u4e86\u5167\u5bb9\u7684\u5be6\u7528\u6027\u548c\u6613\u8b80\u6027\uff0c\u7b26\u5408SEO\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift \u7a0b\u5f0f\u6559\u5b78\uff1a2025 \u6700\u65b0\u5b57\u5178\u8207\u9663\u5217\u6392\u5e8f&#8230;<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1d","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":16,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":7571,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/75\/revisions\/7571"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}