{"id":1177,"date":"2022-06-30T22:32:24","date_gmt":"2022-06-30T14:32:24","guid":{"rendered":"https:\/\/www.mustenaka.cn\/?p=1177"},"modified":"2022-07-20T21:59:23","modified_gmt":"2022-07-20T13:59:23","slug":"golang-custom-struct-sorting","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2022\/06\/30\/golang-custom-struct-sorting\/","title":{"rendered":"golang \u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6392\u5e8f"},"content":{"rendered":"<h1>Golang \u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6392\u5e8f<\/h1>\n<blockquote><p>\u6ce8\u610f\uff0c\u672c\u6587\u4e3b\u8981\u662f\u7814\u7a76Golang\u5982\u4f55\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6392\u5e8f\uff0c\u800c\u4e0d\u662fGolang\u5177\u4f53\u5b9e\u73b0\u4e00\u4e2a\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b\u7684\u3002<\/p><\/blockquote>\n<p>\u4f7f\u7528VSCode\u7f16\u5199sort\u65b9\u6cd5\uff0c\u51fa\u73b0\u7684\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n<div><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/www.mustenaka.cn\/wp-content\/uploads\/2022\/06\/01Y0_Y3PJH26M7PB3-300x80.png\" alt=\"\" width=\"795\" height=\"212\" class=\"alignnone wp-image-1179\" srcset=\"https:\/\/www.mustenaka.cn\/wp-content\/uploads\/2022\/06\/01Y0_Y3PJH26M7PB3-300x80.png 300w, https:\/\/www.mustenaka.cn\/wp-content\/uploads\/2022\/06\/01Y0_Y3PJH26M7PB3-768x204.png 768w, https:\/\/www.mustenaka.cn\/wp-content\/uploads\/2022\/06\/01Y0_Y3PJH26M7PB3.png 794w\" sizes=\"(max-width: 795px) 100vw, 795px\" \/><\/div>\n<div>\u5176\u4e2dSortBy\u8868\u793a\u5b9a\u4e49\u6570\u636e\u96c6\u5408\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\uff0c\u548c\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u624b\u52a8\u8bbe\u8ba1Len()\u957f\u5ea6\u83b7\u53d6\u51fd\u6570\uff0cSwap()\u4ea4\u6362\u51fd\u6570\uff0c\u4ee5\u53caLess()\u6bd4\u8f83\u51fd\u6570\uff0c\u8fd9\u53ef\u8c13\u662f\u76f8\u5f53\u7684\u8ba9\u4ece\u522b\u7684\u8bed\u8a00\u8fc7\u5ea6\u800c\u6765\u7684\u4eba\u611f\u89c9\u4e0d\u8212\u670d\uff0c\u522b\u7684\u8bed\u8a00\u90fd\u662f\u81ea\u5b9a\u4e49\u4e00\u4e2a\u4ea4\u6362\u89c4\u5219\u5c31\u884c\u4e86\u3002<\/div>\n<h5>\u4ee3\u7801\u53c2\u8003<\/h5>\n<p>\u4f9d\u636eId\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u7684\u6392\u5e8f\u3002<\/p>\n<div>\n<pre class=\"pure-highlightjs\"><code class=\"\" null=\"\">package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"sort\"\r\n)\r\n\r\ntype MyStructType struct {\r\n\tId   uint\r\n\tName string\r\n\tDate string\r\n}\r\n\r\ntype SortBy []MyStructType\r\n\r\nfunc (a SortBy) Len() int      { return len(a) }\r\nfunc (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }\r\nfunc (a SortBy) Less(i, j int) bool {\r\n\t\/\/ \u57fa\u4e8eID\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\r\n\treturn a[i].Id &lt; a[j].Id\r\n}\r\n\r\nfunc main() {\r\n\t\/\/ \u5982\u4e0b\uff1a\u4e3a\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u7684\u4f8b\u5b50\r\n\t\/\/ \u521b\u5efa\u4e00\u4e2a\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u5e76\u6dfb\u52a0\u6570\u636e\r\n\tvar myStructs []MyStructType\r\n\tmyStructs = append(myStructs, MyStructType{Id: 1, Name: \"a\", Date: \"2022-03-17\"})\r\n\tmyStructs = append(myStructs, MyStructType{Id: 3, Name: \"b\", Date: \"2022-03-18\"})\r\n\tmyStructs = append(myStructs, MyStructType{Id: 2, Name: \"c\", Date: \"2019-03-17\"})\r\n\tmyStructs = append(myStructs, MyStructType{Id: 5, Name: \"d\", Date: \"1992-03-17\"})\r\n\tmyStructs = append(myStructs, MyStructType{Id: 4, Name: \"e\", Date: \"2022-07-01\"})\r\n\r\n\t\/\/ \u8f93\u51fa\u7ed3\u6784\u4f53\r\n\tfor _, v := range myStructs {\r\n\t\tfmt.Println(v)\r\n\t}\r\n\r\n\t\/\/ \u5bf9\u7ed3\u6784\u4f53\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\r\n\tsort.Sort(SortBy(myStructs))\r\n\r\n\t\/\/ \u518d\u6b21\u8f93\u51fa\u7ed3\u6784\u4f53\r\n\tfor _, v := range myStructs {\r\n\t\tfmt.Println(v)\r\n\t}\r\n}\r\n<\/code><\/pre>\n<\/div>\n<h5>\u57fa\u4e8e\u65f6\u95f4\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/h5>\n<p>\u53ea\u9700\u8981\u628aLess()\u51fd\u6570\u4e2d\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff08\u5e38\u7528\u4e8e\u4eceSQL\u4e2d\u83b7\u53d6\u7684\u6570\u636e\uff0c\u4e0d\u5f97\u4e0d\u63d0Go\u7684\u8fd9\u4e2aTime\u7c7b\u578b\u548cSQL\u7684\u517c\u5bb9\u667a\u80fd\u5316\u4e0a\u5b9e\u5728\u662f\u592a\u4e00\u8a00\u96be\u5c3d\u4e86\uff09\u4e2d\u7684data\u7c7b\u578b\uff0c\u901a\u8fc7time.Pares()\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\uff0c\u53d8\u6210\u65f6\u95f4\u7c7b\u578b\uff0c\u518d\u901a\u8fc7Before()\uff0cEqual\u7b49\u6bd4\u8f83\u624b\u6bb5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u4f8b\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" null=\"\">func (a SortBy) Less(i, j int) bool {\r\n\td1, _ := time.Parse(constant.MysqlDataTimeLayout, a[i].RecordDate)\r\n\td2, _ := time.Parse(constant.MysqlDataTimeLayout, a[j].RecordDate)\r\n\r\n\tt1, _ := time.Parse(constant.TimeLayout, a[i].RecordTime)\r\n\tt2, _ := time.Parse(constant.TimeLayout, a[j].RecordTime)\r\n\r\n        \/\/ \u5982\u679c\u65f6\u95f4\u65e5\u671f\u76f8\u7b49\uff0c\u518d\u6bd4\u8f83\u8fd9\u4e2a\u65e5\u671f\u4e2d\u7684\u5177\u4f53\u65f6\u95f4\r\n\tif d1.Equal(d2) {\r\n\t\treturn t1.Before(t2)\r\n\t} else {\r\n\t\treturn d1.Before(d2)\r\n\t}\r\n}<\/code><\/pre>\n<div>\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u4e8e\u53cd\u5c04\u673a\u5236\u6539\u8fdb\u7684\u7b80\u5355\u81ea\u52a8\u6392\u5e8f\u5e93\uff0c\u76ee\u524d\u6b63\u5728\u5efa\u8bbe\u4e2d\uff0c\u53ef\u4ee5\u7ffb\u9605\u8fd9\u7bc7\u535a\u5ba2\u4e86\u89e3\u8be6\u60c5\u3002<\/div>\n<div><a href=\"https:\/\/www.mustenaka.cn\/index.php\/2022\/07\/20\/simplifiedversionofgolangsort-go-simplesort\/\">https:\/\/www.mustenaka.cn\/index.php\/2022\/07\/20\/simplifiedversionofgolangsort-go-simplesort\/<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Golang \u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6392\u5e8f \u6ce8\u610f\uff0c\u672c\u6587\u4e3b\u8981\u662f\u7814\u7a76Golang\u5982\u4f55\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6392\u5e8f\uff0c\u800c\u4e0d\u662fGola [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[113],"class_list":["post-1177","post","type-post","status-publish","format-standard","hentry","category-go","tag-golang"],"views":989,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1177"}],"version-history":[{"count":9,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1177\/revisions"}],"predecessor-version":[{"id":1194,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1177\/revisions\/1194"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}