{"id":57852,"date":"2024-10-10T21:41:20","date_gmt":"2024-10-10T13:41:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57852\/"},"modified":"2024-10-10T21:41:20","modified_gmt":"2024-10-10T13:41:20","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%8e%92%e5%ba%8f%ef%bc%88%e5%80%9f%e5%8a%a9-sort-interface-%e6%8e%a5%e5%8f%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57852\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u6392\u5e8f\uff08\u501f\u52a9 sort.Interface \u63a5\u53e3\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%8e%92%e5%ba%8f%ef%bc%88%e5%80%9f%e5%8a%a9-sort-interface-%e6%8e%a5%e5%8f%a3%ef%bc%89-khDdFZyLrt\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u6392\u5e8f\uff08\u501f\u52a9 sort.Interface \u63a5\u53e3\uff09<\/h1>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u6392\u5e8f\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0d\u6578\u5b57\u3001\u5b57\u4e32\u9084\u662f\u81ea\u5b9a\u7fa9\u7d50\u69cb\u9ad4\u9032\u884c\u6392\u5e8f\uff0cGo \u63d0\u4f9b\u4e86\u4e00\u500b\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 <code>sort.Interface<\/code> \u63a5\u53e3\u4f86\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u7bc4\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-sort-interface%ef%bc%9f-khDdFZyLrt\">\u4ec0\u9ebc\u662f sort.Interface\uff1f<\/h2>\n<p><code>sort.Interface<\/code> \u662f Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u4e2d\u7684\u4e00\u500b\u63a5\u53e3\uff0c\u5b9a\u7fa9\u4e86\u4e09\u500b\u65b9\u6cd5\uff0c\u9019\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u81ea\u5b9a\u7fa9\u985e\u578b\u53ef\u4ee5\u88ab\u6392\u5e8f\u3002\u9019\u4e09\u500b\u65b9\u6cd5\u5206\u5225\u662f\uff1a<\/p>\n<ul>\n<li><code>Len() int<\/code>\uff1a\u8fd4\u56de\u96c6\u5408\u7684\u9577\u5ea6\u3002<\/li>\n<li><code>Less(i, j int) bool<\/code>\uff1a\u6bd4\u8f03\u96c6\u5408\u4e2d\u7d22\u5f15\u70ba <code>i<\/code> \u548c <code>j<\/code> \u7684\u5143\u7d20\uff0c\u82e5 <code>i<\/code> \u7684\u5143\u7d20\u61c9\u8a72\u6392\u5728 <code>j<\/code> \u524d\u9762\uff0c\u5247\u8fd4\u56de <code>true<\/code>\u3002<\/li>\n<li><code>Swap(i, j int)<\/code>\uff1a\u4ea4\u63db\u96c6\u5408\u4e2d\u7d22\u5f15\u70ba <code>i<\/code> \u548c <code>j<\/code> \u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%87%aa%e5%ae%9a%e7%be%a9%e6%8e%92%e5%ba%8f%e7%af%84%e4%be%8b-khDdFZyLrt\">\u81ea\u5b9a\u7fa9\u6392\u5e8f\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u81ea\u5b9a\u7fa9\u7d50\u69cb\u9ad4\u7684\u6392\u5e8f\u7bc4\u4f8b\u3002\u6211\u5011\u5c07\u5275\u5efa\u4e00\u500b <code>Person<\/code> \u7d50\u69cb\u9ad4\uff0c\u4e26\u6839\u64da\u5e74\u9f61\u9032\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\n\/\/ \u5b9a\u7fa9 Person \u7d50\u69cb\u9ad4\ntype Person struct {\n    Name string\n    Age  int\n}\n\n\/\/ \u5b9a\u7fa9\u4e00\u500b\u985e\u578b\u4f86\u5be6\u73fe sort.Interface\ntype ByAge []Person\n\n\/\/ \u5be6\u73fe Len \u65b9\u6cd5\nfunc (a ByAge) Len() int {\n    return len(a)\n}\n\n\/\/ \u5be6\u73fe Less \u65b9\u6cd5\nfunc (a ByAge) Less(i, j int) bool {\n    return a[i].Age &lt; a[j].Age\n}\n\n\/\/ \u5be6\u73fe Swap \u65b9\u6cd5\nfunc (a ByAge) Swap(i, j int) {\n    a[i], a[j] = a[j], a[i]\n}\n\nfunc main() {\n    \/\/ \u5275\u5efa\u4e00\u500b Person \u5217\u8868\n    people := []Person{\n        {\"Alice\", 30},\n        {\"Bob\", 25},\n        {\"Charlie\", 35},\n    }\n\n    \/\/ \u4f7f\u7528 sort.Sort \u9032\u884c\u6392\u5e8f\n    sort.Sort(ByAge(people))\n\n    \/\/ \u8f38\u51fa\u6392\u5e8f\u5f8c\u7684\u7d50\u679c\n    for _, person := range people {\n        fmt.Println(person.Name, person.Age)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b <code>Person<\/code> \u7d50\u69cb\u9ad4\uff0c\u7136\u5f8c\u5275\u5efa\u4e86\u4e00\u500b <code>ByAge<\/code> \u985e\u578b\u4f86\u5be6\u73fe <code>sort.Interface<\/code>\u3002\u63a5\u8457\uff0c\u6211\u5011\u5be6\u73fe\u4e86\u4e09\u500b\u65b9\u6cd5\uff0c\u6700\u5f8c\u4f7f\u7528 <code>sort.Sort<\/code> \u9032\u884c\u6392\u5e8f\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-sort-slice-%e9%80%b2%e8%a1%8c%e7%b0%a1%e5%8c%96%e6%8e%92%e5%ba%8f-khDdFZyLrt\">\u4f7f\u7528 sort.Slice \u9032\u884c\u7c21\u5316\u6392\u5e8f<\/h2>\n<p>\u5f9e Go 1.8 \u958b\u59cb\uff0cGo \u8a9e\u8a00\u9084\u63d0\u4f9b\u4e86 <code>sort.Slice<\/code> \u51fd\u6578\uff0c\u9019\u4f7f\u5f97\u6392\u5e8f\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>sort.Slice<\/code> \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\nfunc main() {\n    people := []Person{\n        {\"Alice\", 30},\n        {\"Bob\", 25},\n        {\"Charlie\", 35},\n    }\n\n    \/\/ \u4f7f\u7528 sort.Slice \u9032\u884c\u6392\u5e8f\n    sort.Slice(people, func(i, j int) bool {\n        return people[i].Age &lt; people[j].Age\n    })\n\n    for _, person := range people {\n        fmt.Println(person.Name, person.Age)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4e0d\u518d\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u985e\u578b\u4f86\u5be6\u73fe <code>sort.Interface<\/code>\uff0c\u800c\u662f\u76f4\u63a5\u5728 <code>sort.Slice<\/code> \u4e2d\u63d0\u4f9b\u6bd4\u8f03\u51fd\u6578\uff0c\u9019\u6a23\u4f7f\u5f97\u4ee3\u78bc\u66f4\u52a0\u7c21\u6f54\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-khDdFZyLrt\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u901a\u904e\u5be6\u73fe <code>sort.Interface<\/code> \u63a5\u53e3\u6216\u4f7f\u7528 <code>sort.Slice<\/code> \u51fd\u6578\u4f86\u8f15\u9b06\u5b8c\u6210\u3002\u9019\u4e9b\u65b9\u6cd5\u4e0d\u50c5\u9748\u6d3b\uff0c\u800c\u4e14\u80fd\u5920\u9069\u61c9\u5404\u7a2e\u6578\u64da\u985e\u578b\u7684\u6392\u5e8f\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0d\u57fa\u672c\u6578\u64da\u985e\u578b\u9084\u662f\u81ea\u5b9a\u7fa9\u7d50\u69cb\u9ad4\uff0cGo \u90fd\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u7684 sort.Interface \u63a5\u53e3\u9032\u884c\u6392\u5e8f\uff0c\u638c\u63e1\u81ea\u5b9a\u7fa9\u6392\u5e8f\u908f\u8f2f\u7684\u6280\u5de7\u8207\u5be6\u8e10\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57852","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57852","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=57852"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57852\/revisions"}],"predecessor-version":[{"id":57853,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57852\/revisions\/57853"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}