{"id":41292,"date":"2024-12-01T11:10:14","date_gmt":"2024-12-01T03:10:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41292\/"},"modified":"2024-12-01T11:10:14","modified_gmt":"2024-12-01T03:10:14","slug":"%e6%8c%89%e6%95%b0%e5%ad%97%e5%92%8c%e5%ad%97%e6%af%8d%e9%a1%ba%e5%ba%8f%e5%af%b9%e5%9f%ba%e4%ba%8e%e7%bb%93%e6%9e%84%e7%9a%84%e5%88%87%e7%89%87%e8%bf%9b%e8%a1%8c%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41292\/","title":{"rendered":"\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\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>\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\u5e8f<\/span><\/p>\n<h1>\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\u5e8f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 18:33:32<\/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\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\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\u50cf\u8fd9\u6837\u7684\u7ed3\u6784\u5207\u7247\u548c\u8fd9\u4e2a\u6570\u636e<\/p>\n<pre>type interval struct{\n    number     float64\n    coordinate string\n}\n\nfunc getintervals() []interval {\n\n    data := []interval{\n        {number: 1, coordinate: \"x\"},\n        {number: 8, coordinate: \"y\"},\n        {number: 2, coordinate: \"x\"},\n        {number: 5, coordinate: \"y\"},\n        {number: 5, coordinate: \"x\"},\n        {number: 6, coordinate: \"y\"},\n        {number: 3, coordinate: \"x\"},\n        {number: 7, coordinate: \"y\"},\n    }\n    return data\n}\n\n<\/pre>\n<p>\u6211\u7684\u95ee\u9898\u662f\u5982\u4f55\u6309 <code>number<\/code> \u548c <code>coordinate<\/code> \u5bf9 <code>[]interval<\/code> \u8fdb\u884c\u6392\u5e8f\uff1f<\/p>\n<p>\u6211\u5c1d\u8bd5\u8fc7\u4ee5\u4e0b\u6392\u5e8f\u65b9\u6cd5\uff0c\u4f46\u5b83\u5e76\u4e0d\u7b26\u5408\u6211\u7684\u671f\u671b<\/p>\n<pre>data := getintervals()\n\/\/ sort method that i use\nsort.slice(data, func(i, j int) bool {\n    return data[i].number &lt; data[j].number\n})\n<\/pre>\n<pre>[{1 x} {2 x} {3 x} {5 y} {5 x} {6 y} {7 y} {8 y}] \/\/ result\n[{1 x} {2 x} {3 x} {5 x} {5 y} {6 y} {7 y} {8 y}] \/\/ expectation<\/pre>\n<p>\u5dee\u5f02\uff1a <code>{5 y} {5 x}<\/code> \u5e94\u8be5\u662f <code>{5 x} {5 y}<\/code><\/p>\n<p>\u6211\u7684\u9884\u671f\u7ed3\u679c\u4e0epython\u7684\u51fd\u6570<code>sort<\/code>\u7c7b\u4f3c \u6211\u611f\u8c22\u4efb\u4f55\u5e2e\u52a9<\/p>\n<p>\u6f14\u793a<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u5c5e\u6027 <code>number<\/code> \u76f8\u7b49\u7684\u60c5\u51b5\u4e0b\uff0c\u60a8\u7684\u6bd4\u8f83\u5668\u51fd\u6570\u4e0d\u4f1a\u6bd4\u8f83\u5c5e\u6027 <code>coordinate<\/code>\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c{5, x} \u548c {5, y} \u7684\u4f4d\u7f6e\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002<\/p>\n<p>\u8fd9\u662f\u6bd4\u8f83\u5668\u51fd\u6570\u7684\u66f4\u65b0\u7248\u672c\uff1a<\/p>\n<pre>sort.Slice(data, func(i, j int) bool {\n    if data[i].number != data[j].number {\n        return data[i].number &lt; data[j].number\n    }\n    return data[i].coordinate &lt; data[j].coordinate\n})<\/pre>\n<p>\u53c2\u89c1<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\u5e8f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\u5e8f \u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\u5e8f \u6765\u6e90\uff1astackoverflow 2024-04-19 18:33:32 0\u6d4f\u89c8 \u6536\u85cf \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\u6309\u6570\u5b57\u548c\u5b57\u6bcd\u987a\u5e8f\u5bf9\u57fa\u4e8e\u7ed3\u6784\u7684\u5207\u7247\u8fdb\u884c\u6392\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~ \u95ee\u9898\u5185\u5bb9 \u6211\u6709\u4e00\u4e2a\u50cf\u8fd9\u6837\u7684\u7ed3\u6784\u5207\u7247\u548c\u8fd9\u4e2a\u6570\u636e type interval struct{ number float64 coordinate string } func getintervals() []interval { data := []interval{ {number: 1, coordinate: &#8220;x&#8221;}, {number: 8, coordinate: &#8220;y&#8221;}, {number: 2, coordinate: &#8220;x&#8221;}, {number: 5, coordinate: &#8220;y&#8221;}, {number: 5, coordinate: &#8220;x&#8221;}, {number: 6, coordinate: &#8220;y&#8221;}, {number: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41292","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41292","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=41292"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41292\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}