{"id":5162,"date":"2023-01-05T12:05:56","date_gmt":"2023-01-05T04:05:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5162"},"modified":"2023-01-05T12:05:56","modified_gmt":"2023-01-05T04:05:56","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e4%b8%ad%e7%9a%84%e6%95%b8%e7%b5%84%e6%8e%92%e5%ba%8f%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e4%b8%ad%e7%9a%84%e6%95%b8%e7%b5%84%e6%8e%92%e5%ba%8f%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u6578\u7d44\u6392\u5e8f\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6578\u7d44\u7684\u6392\u5e8f, Array Sort\"><\/p>\n<h1>TypeScript \u6578\u7d44\u7684\u6392\u5e8f(Array Sort)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u53ef\u4ee5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 TypeScript \u4f86\u6392\u5e8f\u6578\u7d44\u3002<\/p>\n<h2>TypeScript \u6578\u7d44\u7684\u6392\u5e8f<\/h2>\n<p>TypeScript \u63d0\u4f9b\u4e86\u4e00\u500b Array.sort() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\u3002\u5b83\u63a5\u53d7\u4e00\u500b\u6bd4\u8f03\u51fd\u6578\u4f5c\u70ba\u53c3\u6578\uff0c\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5143\u7d20\u7684\u5927\u5c0f\u3002\u5982\u679c\u6bd4\u8f03\u51fd\u6578\u7684\u8fd4\u56de\u503c\u5c0f\u65bc 0\uff0c\u5247\u7b2c\u4e00\u500b\u5143\u7d20\u6392\u5728\u7b2c\u4e8c\u500b\u5143\u7d20\u4e4b\u524d\uff1b\u5982\u679c\u6bd4\u8f03\u51fd\u6578\u7684\u8fd4\u56de\u503c\u7b49\u65bc 0\uff0c\u5247\u5169\u500b\u5143\u7d20\u7684\u76f8\u5c0d\u4f4d\u7f6e\u4e0d\u8b8a\uff1b\u5982\u679c\u6bd4\u8f03\u51fd\u6578\u7684\u8fd4\u56de\u503c\u5927\u65bc 0\uff0c\u5247\u7b2c\u4e00\u500b\u5143\u7d20\u6392\u5728\u7b2c\u4e8c\u500b\u5143\u7d20\u4e4b\u5f8c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Array.sort() \u65b9\u6cd5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet numbers = [5, 1, 7, 3, 9];\n\nnumbers.sort((a, b) => a - b);\n\nconsole.log(numbers); \/\/ [1, 3, 5, 7, 9]\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u6578\u7d44 numbers\uff0c\u5176\u4e2d\u5305\u542b\u4e86 5 \u500b\u6578\u5b57\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 Array.sort() \u65b9\u6cd5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u50b3\u5165\u4e00\u500b\u6bd4\u8f03\u51fd\u6578\uff0c\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5143\u7d20\u7684\u5927\u5c0f\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u6392\u5e8f\u5f8c\u7684\u6578\u7d44\u8f38\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7d50\u679c\u70ba [1, 3, 5, 7, 9]\u3002<\/p>\n<h2>\u5c0d\u6578\u7d44\u9032\u884c\u5012\u5e8f\u6392\u5e8f<\/h2>\n<p>\u5982\u679c\u6211\u5011\u60f3\u5c0d\u6578\u7d44\u9032\u884c\u5012\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Array.reverse() \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet numbers = [5, 1, 7, 3, 9];\n\nnumbers.sort((a, b) => b - a);\n\nconsole.log(numbers); \/\/ [9, 7, 5, 3, 1]\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 Array.sort() \u65b9\u6cd5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u50b3\u5165\u4e00\u500b\u6bd4\u8f03\u51fd\u6578\uff0c\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5143\u7d20\u7684\u5927\u5c0f\u3002\u6bd4\u8f03\u51fd\u6578\u7684\u8fd4\u56de\u503c\u70ba b &#8211; a\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c0d\u6578\u7d44\u9032\u884c\u5012\u5e8f\u6392\u5e8f\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u6392\u5e8f\u5f8c\u7684\u6578\u7d44\u8f38\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7d50\u679c\u70ba [9, 7, 5, 3, 1]\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 TypeScript \u4f86\u6392\u5e8f\u6578\u7d44\u3002\u6211\u5011\u4f7f\u7528 Array.sort() \u65b9\u6cd5\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u50b3\u5165\u4e00\u500b\u6bd4\u8f03\u51fd\u6578\uff0c\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5143\u7d20\u7684\u5927\u5c0f\u3002\u6211\u5011\u9084\u4ecb\u7d39\u4e86\u5982\u4f55\u5c0d\u6578\u7d44\u9032\u884c\u5012\u5e8f\u6392\u5e8f\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u66f4\u591a\u8cc7\u6e90<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc TypeScript \u6578\u7d44\u7684\u6392\u5e8f\u7684\u77e5\u8b58\uff0c\u53ef\u4ee5\u53c3\u8003\u4ee5\u4e0b\u8cc7\u6e90\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/advanced-types.html\" target=\"_blank\" rel=\"noopener\">TypeScript \u624b\u518a &#8211; \u9032\u968e\u985e\u578b<\/a><\/li>\n<li><a href=\"https:\/\/www.tutorialspoint.com\/typescript\/typescript_arrays.htm\" target=\"_blank\" rel=\"noopener\">TypeScript \u6559\u7a0b &#8211; \u6578\u7d44<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u6578\u7d44\u6392\u5e8f\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u5c0d\u6578\u7d44\u9032\u884c\u6392\u5e8f\uff0c\u4ee5\u6eff\u8db3\u61c9\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1lg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5162","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=5162"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5162\/revisions"}],"predecessor-version":[{"id":5163,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5162\/revisions\/5163"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}