{"id":207741,"date":"2025-07-08T12:33:29","date_gmt":"2025-07-08T04:33:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207741\/"},"modified":"2025-07-08T12:33:29","modified_gmt":"2025-07-08T04:33:29","slug":"%e5%b0%86%e5%88%87%e7%89%87%e8%b0%83%e6%95%b4%e4%b8%ba%e6%96%b0%e9%a1%ba%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207741\/","title":{"rendered":"\u5c06\u5207\u7247\u8c03\u6574\u4e3a\u65b0\u987a\u5e8f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5c06\u5207\u7247\u8c03\u6574\u4e3a\u65b0\u987a\u5e8f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 11:09:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5c06\u5207\u7247\u8c03\u6574\u4e3a\u65b0\u987a\u5e8f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u7c7b\u578b&#8230;<\/p>\n<pre>type mytype struct {\n    job       string `json:\"job\"`\n    cost      string `json:\"cost\"`\n\n}<\/pre>\n<p>\u8fd9\u79cd\u7c7b\u578b\u7684\u5207\u7247&#8230;<\/p>\n<pre>var records []mytype<\/pre>\n<p>\u5b83\u7684\u586b\u5145\u5982\u4e0b&#8230;<\/p>\n<pre>records = append(records, MyType{Job: 100, Cost:234},)\nrecords = append(records, MyType{Job: 101, Cost:4000},)\nrecords = append(records, MyType{Job: 102, Cost:700},)<\/pre>\n<p>\u6211\u60f3\u6309\u6210\u672c\u964d\u5e8f\u5bf9\u201c\u884c\u201d\u8fdb\u884c\u6392\u5e8f\uff0c\u6240\u4ee5&#8230;<\/p>\n<p>records[0].job \u7b49\u4e8e 101 \u8bb0\u5f55[0].\u6210\u672c\u7b49\u4e8e 4000<\/p>\n<p>records[1].job \u7b49\u4e8e 102 \u8bb0\u5f55[1]\u3002\u6210\u672c\u7b49\u4e8e 700<\/p>\n<p>records[2].job \u7b49\u4e8e 100 \u8bb0\u5f55[2]\u3002\u6210\u672c\u7b49\u4e8e 234<\/p>\n<p>\u5728 c# \u4e2d\uff0c\u6211\u901a\u5e38\u4f1a\u4f7f\u7528 linq\uff0c\u4f46\u6211\u662f\u65b0\u624b\uff0c\u4e0d\u77e5\u9053\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#8230;<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u9996\u5148 json \u7c7b\u578b\u65e0\u6548\u3002\u6210\u672c\u5e94\u4e3a <code>int<\/code> \u7c7b\u578b<\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\u4ee3\u7801<\/p>\n<pre>import (\n    \"fmt\"\n    \"sort\"\n)\n\ntype records []mytype\n\ntype mytype struct {\n    job  int `json:\"job\"`\n    cost int `json:\"cost\"`\n}\n\nfunc (r records) len() int           { return len(r) }\nfunc (r records) swap(i, j int)      { r[i], r[j] = r[j], r[i] }\nfunc (r records) less(i, j int) bool { return r[i].cost &gt; r[j].cost }\n\nfunc main() {\n\n    records := make(records, 0, 1000)\n    records = append(records, mytype{job: 100, cost: 234})\n    records = append(records, mytype{job: 101, cost: 4000})\n    records = append(records, mytype{job: 102, cost: 700})\n\n    sort.sort(records)\n    fmt.println(records)\n}<\/pre>\n<p>\u6216\u8005\u6ca1\u6709\u81ea\u5b9a\u4e49\u5207\u7247<\/p>\n<pre>type mytype struct {\n    job  int `json:\"job\"`\n    cost int `json:\"cost\"`\n}\n\nfunc main() {\n\n    records := make([]mytype, 0, 1000)\n    records = append(records, mytype{job: 100, cost: 234})\n    records = append(records, mytype{job: 101, cost: 4000})\n    records = append(records, mytype{job: 102, cost: 700})\n\n    sort.slice(records, func(i, j int) bool {\n        return records[i].cost &gt; records[j].cost\n    })\n    fmt.println(records)\n}<\/pre>\n<p>\u8f93\u51fa<\/p>\n<pre>[{101 4000} {102 700} {100 234}]<\/pre>\n<p>\u53c2\u8003\u53f7<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5c06\u5207\u7247\u8c03\u6574\u4e3a\u65b0\u987a\u5e8f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207741","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207741"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207741\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}