{"id":1253,"date":"2020-04-18T11:13:57","date_gmt":"2020-04-18T03:13:57","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=1253"},"modified":"2023-12-04T15:48:36","modified_gmt":"2023-12-04T07:48:36","slug":"go%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80-%e5%9f%ba%e6%9c%ac%e6%8e%92%e5%ba%8f%e5%92%8cslice%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=1253","title":{"rendered":"go\u8bed\u8a00\u57fa\u7840\u2014\u2014\u57fa\u672c\u6392\u5e8f\u548cslice\u6392\u5e8f"},"content":{"rendered":"<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/itimetraveler.github.io\/2016\/09\/07\/%E3%80%90Go%E8%AF%AD%E8%A8%80%E3%80%91%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%8E%92%E5%BA%8F%E5%92%8C%20slice%20%E6%8E%92%E5%BA%8F\/#3%E3%80%81%E8%BF%9B%E4%B8%80%E6%AD%A5%E5%B0%81%E8%A3%85\" title=\"\u3010Go\u8bed\u8a00\u3011\u57fa\u672c\u7c7b\u578b\u6392\u5e8f\u548c slice \u6392\u5e8f\">\u3010Go\u8bed\u8a00\u3011\u57fa\u672c\u7c7b\u578b\u6392\u5e8f\u548c slice \u6392\u5e8f<\/a><\/p>\n<blockquote><p>\n  Go \u662f\u901a\u8fc7 sort \u5305\u63d0\u4f9b\u6392\u5e8f\u548c\u641c\u7d22\uff0c\u56e0\u4e3a Go \u6682\u65f6\u4e0d\u652f\u6301\u6cdb\u578b\uff08\u5c06\u6765\u4e5f\u4e0d\u597d\u8bf4\u652f\u4e0d\u652f\u6301\uff09\uff0c\u6240\u4ee5\uff0cGo \u7684 sort \u548c search \u4f7f\u7528\u8d77\u6765\u8ddf\u7c7b\u578b\u662f\u6709\u5173\u7684\uff0c\u6216\u662f\u9700\u8981\u50cf c \u4e00\u6837\u5199\u6bd4\u8f83\u51fd\u6570\u7b49\uff0c\u7a0d\u5fae\u663e\u5f97\u4e5f\u4e0d\u662f\u5f88\u65b9\u4fbf\u3002\n<\/p><\/blockquote>\n<h3>\u5f15\u8a00<\/h3>\n<p>Go \u7684\u6392\u5e8f\u601d\u8def\u548c C \u548c C++ \u6709\u4e9b\u5dee\u522b\u3002 C \u9ed8\u8ba4\u662f\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c C++ \u662f\u5bf9\u4e00\u4e2a\u5e8f\u5217\u8fdb\u884c\u6392\u5e8f\uff0c Go \u5219\u66f4\u5bbd\u6cdb\u4e00\u4e9b\uff0c\u5f85\u6392\u5e8f\u7684\u53ef\u4ee5\u662f\u4efb\u4f55\u5bf9\u8c61\uff0c \u867d\u7136\u5f88\u591a\u60c5\u51b5\u4e0b\u662f\u4e00\u4e2a slice (\u5206\u7247\uff0c \u7c7b\u4f3c\u4e8e\u6570\u7ec4)\uff0c\u6216\u662f\u5305\u542b slice \u7684\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u6392\u5e8f(\u63a5\u53e3)\u7684\u4e09\u4e2a\u8981\u7d20\uff1a<br \/>\n&#8211; \u5f85\u6392\u5e8f\u5143\u7d20\u4e2a\u6570 n \uff1b<br \/>\n&#8211; \u7b2c i \u548c\u7b2c j \u4e2a\u5143\u7d20\u7684\u6bd4\u8f83\u51fd\u6570 cmp \uff1b<br \/>\n&#8211; \u7b2c i \u548c \u7b2c j \u4e2a\u5143\u7d20\u7684\u4ea4\u6362 swap \uff1b<\/p>\n<p>\u4e4d\u4e00\u770b\u6761\u4ef6 3 \u662f\u591a\u4f59\u7684\uff0c c \u548c c++ \u90fd\u4e0d\u63d0\u4f9b swap \u3002 c \u7684 qsort \u7684\u7528\u6cd5\uff1a qsort(data, n, sizeof(int), cmp_int); data \u662f\u8d77\u59cb\u5730\u5740\uff0c n \u662f\u5143\u7d20\u4e2a\u6570\uff0c sizeof(int) \u662f\u6bcf\u4e2a\u5143\u7d20\u7684\u5927\u5c0f\uff0c cmp_int \u662f\u4e00\u4e2a\u6bd4\u8f83\u4e24\u4e2a int \u7684\u51fd\u6570\u3002<\/p>\n<p>c++ \u7684 sort \u7684\u7528\u6cd5\uff1a sort(data, data+n, cmp_int); data \u662f\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c data+n \u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c cmp_int \u662f\u6bd4\u8f83\u51fd\u6570\u3002<\/p>\n<h3>\u57fa\u672c\u7c7b\u578b\u6392\u5e8f<\/h3>\n<h4>1\u3001\u5347\u5e8f\u6392\u5e8f<\/h4>\n<p>\u5bf9\u4e8e int \u3001 float64 \u548c string \u6570\u7ec4\u6216\u662f\u5206\u7247\u7684\u6392\u5e8f\uff0c go \u5206\u522b\u63d0\u4f9b\u4e86 sort.Ints() \u3001 sort.Float64s() \u548c sort.Strings() \u51fd\u6570\uff0c \u9ed8\u8ba4\u90fd\u662f\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\nfunc main() {\n    intList := [] int {2, 4, 3, 5, 7, 6, 9, 8, 1, 0}\n    float8List := [] float64 {4.2, 5.9, 12.3, 10.0, 50.4, 99.9, 31.4, 27.81828, 3.14}\n    stringList := [] string {\"a\", \"c\", \"b\", \"d\", \"f\", \"i\", \"z\", \"x\", \"w\", \"y\"}\n\n    sort.Ints(intList)\n    sort.Float64s(float8List)\n    sort.Strings(stringList)\n\n    fmt.Printf(\"%v\\n%v\\n%v\\n\", intList, float8List, stringList)\n\n}\n<\/code><\/pre>\n<h4>2\u3001\u964d\u5e8f\u6392\u5e8f<\/h4>\n<p>int \u3001 float64 \u548c string \u90fd\u6709\u9ed8\u8ba4\u7684\u5347\u5e8f\u6392\u5e8f\u51fd\u6570\uff0c \u73b0\u5728\u95ee\u9898\u662f\u5982\u679c\u964d\u5e8f\u5982\u4f55 \uff1f \u6709\u5176\u4ed6\u8bed\u8a00\u7f16\u7a0b\u7ecf\u9a8c\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u53ea\u9700\u8981\u4ea4\u6362 cmp \u7684\u6bd4\u8f83\u6cd5\u5219\u5c31\u53ef\u4ee5\u4e86\uff0c go \u7684\u5b9e\u73b0\u662f\u7c7b\u4f3c\u7684\uff0c\u7136\u800c\u53c8\u6709\u6240\u4e0d\u540c\u3002 go \u4e2d\u5bf9\u67d0\u4e2a Type \u7684\u5bf9\u8c61 obj \u6392\u5e8f\uff0c \u53ef\u4ee5\u4f7f\u7528 sort.Sort(obj) \u5373\u53ef\uff0c\u5c31\u662f\u9700\u8981\u5bf9 Type \u7c7b\u578b\u7ed1\u5b9a\u4e09\u4e2a\u65b9\u6cd5 \uff1a Len() \u6c42\u957f\u5ea6\u3001Less(i,j) \u6bd4\u8f83\u7b2c i \u548c \u7b2c j \u4e2a\u5143\u7d20\u5927\u5c0f\u7684\u51fd\u6570\u3001 Swap(i,j) \u4ea4\u6362\u7b2c i \u548c\u7b2c j \u4e2a\u5143\u7d20\u7684\u51fd\u6570\u3002sort \u5305\u4e0b\u7684\u4e09\u4e2a\u7c7b\u578b IntSlice \u3001 Float64Slice \u3001 StringSlice \u5206\u522b\u5b9e\u73b0\u4e86\u8fd9\u4e09\u4e2a\u65b9\u6cd5\uff0c \u5bf9\u5e94\u6392\u5e8f\u7684\u662f [] int \u3001 [] float64 \u548c [] string \u3002\u5982\u679c\u671f\u671b\u9006\u5e8f\u6392\u5e8f\uff0c \u53ea\u9700\u8981\u5c06\u5bf9\u5e94\u7684 Less \u51fd\u6570\u7b80\u5355\u4fee\u6539\u4e00\u4e0b\u5373\u53ef\u3002<\/p>\n<p>go \u7684 sort \u5305\u53ef\u4ee5\u4f7f\u7528 sort.Reverse(slice) \u6765\u8c03\u6362 slice.Interface.Less \uff0c\u4e5f\u5c31\u662f\u6bd4\u8f83\u51fd\u6570\uff0c\u6240\u4ee5\uff0c int \u3001 float64 \u548c string \u7684\u9006\u5e8f\u6392\u5e8f\u51fd\u6570\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\nfunc main() {\n    intList := [] int {2, 4, 3, 5, 7, 6, 9, 8, 1, 0}\n    float8List := [] float64 {4.2, 5.9, 12.3, 10.0, 50.4, 99.9, 31.4, 27.81828, 3.14}\n    stringList := [] string {\"a\", \"c\", \"b\", \"d\", \"f\", \"i\", \"z\", \"x\", \"w\", \"y\"}\n\n    sort.Sort(sort.Reverse(sort.IntSlice(intList)))\n    sort.Sort(sort.Reverse(sort.Float64Slice(float8List)))\n    sort.Sort(sort.Reverse(sort.StringSlice(stringList)))\n\n    fmt.Printf(\"%v\\n%v\\n%v\\n\", intList, float8List, stringList)\n}\n<\/code><\/pre>\n<h4>3\u3001\u6df1\u5165\u7406\u89e3\u6392\u5e8f<\/h4>\n<p>sort \u5305\u4e2d\u6709\u4e00\u4e2a sort.Interface \u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u6709\u4e09\u4e2a\u65b9\u6cd5 Len() \u3001 Less(i,j) \u548c Swap(i,j) \u3002 \u901a\u7528\u6392\u5e8f\u51fd\u6570 sort.Sort \u53ef\u4ee5\u6392\u5e8f\u4efb\u4f55\u5b9e\u73b0\u4e86 sort.Inferface \u63a5\u53e3\u7684\u5bf9\u8c61(\u53d8\u91cf)\u3002\u5bf9\u4e8e [] int \u3001[] float64 \u548c [] string \u9664\u4e86\u4f7f\u7528\u7279\u6b8a\u6307\u5b9a\u7684\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6539\u88c5\u8fc7\u7684\u7c7b\u578b IntSclice \u3001 Float64Slice \u548c StringSlice \uff0c \u7136\u540e\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u5bf9\u5e94\u7684 Sort() \u65b9\u6cd5\uff1b\u56e0\u4e3a\u8fd9\u4e09\u79cd\u7c7b\u578b\u4e5f\u5b9e\u73b0\u4e86 sort.Interface \u63a5\u53e3\uff0c \u6240\u4ee5\u53ef\u4ee5\u901a\u8fc7 sort.Reverse \u6765\u8f6c\u6362\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684 Interface.Less \u65b9\u6cd5\u6765\u5b9e\u73b0\u9006\u5411\u6392\u5e8f\uff0c \u8fd9\u5c31\u662f\u524d\u9762\u6700\u540e\u4e00\u4e2a\u6392\u5e8f\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u4e0b\u9762\u4f7f\u7528\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49(\u7528\u6237\u5b9a\u4e49)\u7684 Reverse \u7ed3\u6784\u4f53\uff0c \u800c\u4e0d\u662f sort.Reverse \u51fd\u6570\uff0c \u6765\u5b9e\u73b0\u9006\u5411\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\n\/\/ \u81ea\u5b9a\u4e49\u7684 Reverse \u7c7b\u578b\ntype Reverse struct {\n    sort.Interface    \/\/ \u8fd9\u6837\uff0cReverse\u53ef\u4ee5\u63a5\u7eb3\u4efb\u4f55\u5b9e\u73b0\u4e86sort.Interface\u7684\u5bf9\u8c61\n}\n\n\/\/ Reverse \u53ea\u662f\u5c06\u5176\u4e2d\u7684 Inferface.Less \u7684\u987a\u5e8f\u5bf9\u8c03\u4e86\u4e00\u4e0b\nfunc (r Reverse) Less(i, j int) bool {\n    return r.Interface.Less(j, i)\n}\n\nfunc main() {\n    ints := []int{5, 2, 6, 3, 1, 4}\n\n    sort.Ints(ints)                                     \/\/ \u7279\u6b8a\u6392\u5e8f\u51fd\u6570\uff0c\u5347\u5e8f\n    fmt.Println(\"after sort by Ints:\\t\", ints)\n\n    doubles := []float64{2.3, 3.2, 6.7, 10.9, 5.4, 1.8}\n\n    sort.Float64s(doubles)\n    fmt.Println(\"after sort by Float64s:\\t\", doubles)   \/\/ [1.8 2.3 3.2 5.4 6.7 10.9]\n\n    strings := []string{\"hello\", \"good\", \"students\", \"morning\", \"people\", \"world\"}\n    sort.Strings(strings)\n    fmt.Println(\"after sort by Strings:\\t\", strings)    \/\/ [good hello mornig people students world]\n\n    ipos := sort.SearchInts(ints, -1)    \/\/ int \u641c\u7d22\n    fmt.Printf(\"pos of 5 is %d th\\n\", ipos)\n\n    dpos := sort.SearchFloat64s(doubles, 20.1)    \/\/ float64 \u641c\u7d22\n    fmt.Printf(\"pos of 5.0 is %d th\\n\", dpos)\n\n    fmt.Printf(\"doubles is asc ? %v\\n\", sort.Float64sAreSorted(doubles))\n\n    doubles = []float64{3.5, 4.2, 8.9, 100.98, 20.14, 79.32}\n    \/\/ sort.Sort(sort.Float64Slice(doubles))    \/\/ float64 \u6392\u5e8f\u65b9\u6cd5 2\n    \/\/ fmt.Println(\"after sort by Sort:\\t\", doubles)    \/\/ [3.5 4.2 8.9 20.14 79.32 100.98]\n    (sort.Float64Slice(doubles)).Sort()         \/\/ float64 \u6392\u5e8f\u65b9\u6cd5 3\n    fmt.Println(\"after sort by Sort:\\t\", doubles)       \/\/ [3.5 4.2 8.9 20.14 79.32 100.98]\n\n    sort.Sort(Reverse{sort.Float64Slice(doubles)})    \/\/ float64 \u9006\u5e8f\u6392\u5e8f\n    fmt.Println(\"after sort by Reversed Sort:\\t\", doubles)      \/\/ [100.98 79.32 20.14 8.9 4.2 3.5]\n}\n\n<\/code><\/pre>\n<p>sort.Ints \/ sort.Float64s \/ sort.Strings \u5206\u522b\u6765\u5bf9\u6574\u578b\/\u6d6e\u70b9\u578b\/\u5b57\u7b26\u4e32\u578bslice\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u540e\u662f\u6709\u4e2a\u6d4b\u8bd5\u662f\u5426\u6709\u5e8f\u7684\u51fd\u6570\u3002\u8fd8\u6709\u5206\u522b\u5bf9\u5e94\u7684 search \u51fd\u6570\uff0c\u4e0d\u8fc7\uff0c\u53d1\u73b0\u641c\u7d22\u51fd\u6570\u53ea\u80fd\u5b9a\u4f4d\u5230\u5982\u679c\u5b58\u5728\u7684\u8bdd\u7684\u4f4d\u7f6e\uff0c\u4e0d\u5b58\u5728\u7684\u8bdd\uff0c\u4f4d\u7f6e\u662f\u4e0d\u5bf9\u7684\u3002<\/p>\n<p>\u5173\u4e8e\u4e00\u822c\u7684\u6570\u7ec4\u6392\u5e8f\uff0c\u7a0b\u5e8f\u4e2d\u663e\u793a\u4e86\uff0c\u6709 3 \u79cd\u65b9\u6cd5\uff01\u76ee\u524d\u63d0\u4f9b\u7684\u4e09\u79cd\u7c7b\u578b int\uff0cfloat64 \u548c string \u5448\u73b0\u5bf9\u79f0\u7684\uff0c\u4e5f\u5c31\u662f\u4f60\u6709\u7684\uff0c\u5bf9\u5e94\u7684\u6211\u4e5f\u6709\u3002\u5173\u4e8e\u7ffb\u8f6c\u6392\u5e8f\u6216\u662f\u9006\u5411\u6392\u5e8f\uff0c\u5c31\u662f\u7528\u4e2a\u7ffb\u8f6c\u7ed3\u6784\u4f53\uff0c\u91cd\u5199 Less() \u51fd\u6570\u5373\u53ef\u3002\u4e0a\u9762\u7684 Reverse \u662f\u4e2a\u901a\u7528\u7684\u7ed3\u6784\u4f53\u3002<\/p>\n<p>\u4e0a\u9762\u8bf4\u4e86\u90a3\u4e48\u591a\uff0c \u53ea\u662f\u5bf9\u57fa\u672c\u7c7b\u578b\u8fdb\u884c\u6392\u5e8f\uff0c \u8be5\u5230\u8bf4\u8bf4 struct \u7ed3\u6784\u4f53\u7c7b\u578b\u7684\u6392\u5e8f\u7684\u65f6\u5019\u4e86\uff0c \u5b9e\u9645\u4e2d\u8fd9\u4e2a\u7528\u5f97\u5230\u7684\u4f1a\u66f4\u591a\u3002<\/p>\n<h3>\u7ed3\u6784\u4f53\u7c7b\u578b\u6392\u5e8f<\/h3>\n<p>\u7ed3\u6784\u4f53\u7c7b\u578b\u7684\u6392\u5e8f\u662f\u901a\u8fc7\u4f7f\u7528 sort.Sort(slice) \u5b9e\u73b0\u7684\uff0c \u53ea\u8981 slice \u5b9e\u73b0\u4e86 sort.Interface \u7684\u4e09\u4e2a\u65b9\u6cd5\u5c31\u53ef\u4ee5\u3002 \u867d\u7136\u8fd9\u4e48\u8bf4\uff0c\u4f46\u662f\u6392\u5e8f\u7684\u65b9\u6cd5\u5374\u6709\u90a3\u4e48\u597d\u51e0\u79cd\u3002\u9996\u5148\u4e00\u79cd\u5c31\u662f\u6a21\u62df\u6392\u5e8f [] int \u6784\u9020\u5bf9\u5e94\u7684 IntSlice \u7c7b\u578b\uff0c\u7136\u540e\u5bf9 IntSlice \u7c7b\u578b\u5b9e\u73b0 Interface \u7684\u4e09\u4e2a\u65b9\u6cd5\u3002<\/p>\n<h4>1\u3001\u6a21\u62dfIntSlice\u6392\u5e8f<\/h4>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\n\/\/ \u6309\u7167 Person.Age \u4ece\u5927\u5230\u5c0f\u6392\u5e8f\ntype PersonSlice [] Person\n\nfunc (a PersonSlice) Len() int {         \/\/ \u91cd\u5199 Len() \u65b9\u6cd5\n    return len(a)\n}\nfunc (a PersonSlice) Swap(i, j int){     \/\/ \u91cd\u5199 Swap() \u65b9\u6cd5\n    a[i], a[j] = a[j], a[i]\n}\nfunc (a PersonSlice) Less(i, j int) bool {    \/\/ \u91cd\u5199 Less() \u65b9\u6cd5\uff0c \u4ece\u5927\u5230\u5c0f\u6392\u5e8f\n    return a[j].Age &lt; a[i].Age\n}\n\nfunc main() {\n    people := [] Person{\n        {\"zhang san\", 12},\n        {\"li si\", 30},\n        {\"wang wu\", 52},\n        {\"zhao liu\", 26},\n    }\n\n    fmt.Println(people)\n\n    sort.Sort(PersonSlice(people))    \/\/ \u6309\u7167 Age \u7684\u9006\u5e8f\u6392\u5e8f\n    fmt.Println(people)\n\n    sort.Sort(sort.Reverse(PersonSlice(people)))    \/\/ \u6309\u7167 Age \u7684\u5347\u5e8f\u6392\u5e8f\n    fmt.Println(people)\n\n}\n<\/code><\/pre>\n<p>\u8fd9\u5b8c\u5168\u662f\u4e00\u79cd\u6a21\u62df\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u5982\u679c\u61c2\u4e86 IntSlice \u81ea\u7136\u5c31\u7406\u89e3\u8fd9\u91cc\u4e86\uff0c\u53cd\u8fc7\u6765\uff0c\u7406\u89e3\u4e86\u8fd9\u91cc\u90a3\u4e48 IntSlice \u90a3\u91cc\u4e5f\u5c31\u61c2\u4e86\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\uff1a\u6839\u636e Age \u6392\u5e8f\u9700\u8981\u91cd\u65b0\u5b9a\u4e49 PersonSlice \u65b9\u6cd5\uff0c\u7ed1\u5b9a Len \u3001 Less \u548c Swap \u65b9\u6cd5\uff0c \u5982\u679c\u9700\u8981\u6839\u636e Name \u6392\u5e8f\uff0c \u53c8\u9700\u8981\u91cd\u65b0\u5199\u4e09\u4e2a\u51fd\u6570\uff1b \u5982\u679c\u7ed3\u6784\u4f53\u6709 4 \u4e2a\u5b57\u6bb5\uff0c\u6709\u56db\u79cd\u7c7b\u578b\u7684\u6392\u5e8f\uff0c\u90a3\u4e48\u5c31\u8981\u5199 3 \u00d7 4 = 12 \u4e2a\u65b9\u6cd5\uff0c \u5373\u4f7f\u6709\u4e00\u4e9b\u5b8c\u5168\u662f\u591a\u4f59\u7684\uff0c O__O\u201d\u2026 \u4ed4\u7ec6\u601d\u91cf\u4e00\u4e0b\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6807\u51c6 Age \u6216\u662f Name\uff0c \u771f\u6b63\u4e0d\u540c\u7684\u4f53\u73b0\u5728 Less \u65b9\u6cd5\u4e0a\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06 Less \u62bd\u8c61\u51fa\u6765\uff0c \u6bcf\u79cd\u6392\u5e8f\u7684 Less \u8ba9\u5176\u53d8\u6210\u52a8\u6001\u7684\uff0c\u6bd4\u5982\u4e0b\u9762\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<h4>2\u3001\u5c01\u88c5\u6210 Wrapper<\/h4>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\ntype PersonWrapper struct {                 \/\/\u6ce8\u610f\u6b64\u5904\n    people [] Person\n    by func(p, q * Person) bool\n}\n\nfunc (pw PersonWrapper) Len() int {         \/\/ \u91cd\u5199 Len() \u65b9\u6cd5\n    return len(pw.people)\n}\nfunc (pw PersonWrapper) Swap(i, j int){     \/\/ \u91cd\u5199 Swap() \u65b9\u6cd5\n    pw.people[i], pw.people[j] = pw.people[j], pw.people[i]\n}\nfunc (pw PersonWrapper) Less(i, j int) bool {    \/\/ \u91cd\u5199 Less() \u65b9\u6cd5\n    return pw.by(&amp;pw.people[i], &amp;pw.people[j])\n}\n\nfunc main() {\n    people := [] Person{\n        {\"zhang san\", 12},\n        {\"li si\", 30},\n        {\"wang wu\", 52},\n        {\"zhao liu\", 26},\n    }\n\n    fmt.Println(people)\n\n    sort.Sort(PersonWrapper{people, func (p, q *Person) bool {\n        return q.Age &lt; p.Age    \/\/ Age \u9012\u51cf\u6392\u5e8f\n    }})\n\n    fmt.Println(people)\n    sort.Sort(PersonWrapper{people, func (p, q *Person) bool {\n        return p.Name &lt; q.Name    \/\/ Name \u9012\u589e\u6392\u5e8f\n    }})\n\n    fmt.Println(people)\n\n}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06 [] Person \u548c\u6bd4\u8f83\u7684\u51c6\u5219 cmp \u5c01\u88c5\u5728\u4e86\u4e00\u8d77\uff0c\u5f62\u6210\u4e86 PersonWrapper \u51fd\u6570\uff0c\u7136\u540e\u5728\u5176\u4e0a\u7ed1\u5b9a Len \u3001 Less \u548c Swap \u65b9\u6cd5\u3002 \u5b9e\u9645\u4e0a sort.Sort(pw) \u6392\u5e8f\u7684\u662f pw \u4e2d\u7684 people\uff0c \u8fd9\u5c31\u662f\u524d\u9762\u8bf4\u7684\uff0c go \u7684\u6392\u5e8f\u672a\u5fc5\u5c31\u662f\u9488\u5bf9\u7684\u4e00\u4e2a\u6570\u7ec4\u6216\u662f slice\uff0c \u800c\u53ef\u4ee5\u662f\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u7684\u6570\u7ec4\u6216\u662f slice \u3002<\/p>\n<h4>3\u3001\u8fdb\u4e00\u6b65\u5c01\u88c5<\/h4>\n<p>\u611f\u89c9\u65b9\u6cd5 2 \u5df2\u7ecf\u5f88\u4e0d\u9519\u4e86\uff0c \u552f\u4e00\u4e00\u4e2a\u7f3a\u70b9\u662f\uff0c\u5728 main \u4e2d\u4f7f\u7528\u7684\u65f6\u5019\u66b4\u9732\u4e86 sort.Sort \u7684\u4f7f\u7528\uff0c\u8fd8\u6709\u5c31\u662f PersonWrapper \u7684\u6784\u9020\u3002 \u4e3a\u4e86\u8ba9 main \u4e2d\u4f7f\u7528\u8d77\u6765\u66f4\u4e3a\u65b9\u4fbf\uff0c \u6211\u4eec\u53ef\u4ee5\u518d\u7b80\u5355\u7684\u5c01\u88c5\u4e00\u4e0b\uff0c \u6784\u9020\u4e00\u4e2a SortPerson \u65b9\u6cd5\uff0c \u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\ntype PersonWrapper struct {\n    people [] Person\n    by func(p, q * Person) bool\n}\n\ntype SortBy func(p, q *Person) bool\n\nfunc (pw PersonWrapper) Len() int {         \/\/ \u91cd\u5199 Len() \u65b9\u6cd5\n    return len(pw.people)\n}\nfunc (pw PersonWrapper) Swap(i, j int){         \/\/ \u91cd\u5199 Swap() \u65b9\u6cd5\n    pw.people[i], pw.people[j] = pw.people[j], pw.people[i]\n}\nfunc (pw PersonWrapper) Less(i, j int) bool {    \/\/ \u91cd\u5199 Less() \u65b9\u6cd5\n    return pw.by(&amp;pw.people[i], &amp;pw.people[j])\n}\n\n\/\/ \u5c01\u88c5\u6210 SortPerson \u65b9\u6cd5\nfunc SortPerson(people [] Person, by SortBy){\n    sort.Sort(PersonWrapper{people, by})\n}\n\nfunc main() {\n    people := [] Person{\n        {\"zhang san\", 12},\n        {\"li si\", 30},\n        {\"wang wu\", 52},\n        {\"zhao liu\", 26},\n    }\n\n    fmt.Println(people)\n\n    sort.Sort(PersonWrapper{people, func (p, q *Person) bool {\n        return q.Age &lt; p.Age    \/\/ Age \u9012\u51cf\u6392\u5e8f\n    }})\n\n    fmt.Println(people)\n\n    SortPerson(people, func (p, q *Person) bool {\n        return p.Name &lt; q.Name    \/\/ Name \u9012\u589e\u6392\u5e8f\n    })\n\n    fmt.Println(people)\n\n}\n<\/code><\/pre>\n<p>\u5728\u65b9\u6cd5 2 \u7684\u57fa\u7840\u4e0a\u6784\u9020\u4e86 SortPerson \u51fd\u6570\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u4f20\u8fc7\u53bb\u4e00\u4e2a [] Person \u548c\u4e00\u4e2a cmp \u51fd\u6570\u3002<\/p>\n<h4>4\u3001\u53e6\u4e00\u79cd\u601d\u8def<\/h4>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\ntype Person struct {\n    Name        string\n    Weight      int\n}\n\ntype PersonSlice []Person\n\nfunc (s PersonSlice) Len() int  { return len(s) }\nfunc (s PersonSlice) Swap(i, j int)     { s[i], s[j] = s[j], s[i] }\n\ntype ByName struct{ PersonSlice }    \/\/ \u5c06 PersonSlice \u5305\u88c5\u8d77\u6765\u5230 ByName \u4e2d\n\nfunc (s ByName) Less(i, j int) bool     { return s.PersonSlice[i].Name &lt; s.PersonSlice[j].Name }    \/\/ \u5c06 Less \u7ed1\u5b9a\u5230 ByName \u4e0a\n\n\ntype ByWeight struct{ PersonSlice }    \/\/ \u5c06 PersonSlice \u5305\u88c5\u8d77\u6765\u5230 ByWeight \u4e2d\nfunc (s ByWeight) Less(i, j int) bool   { return s.PersonSlice[i].Weight &lt; s.PersonSlice[j].Weight }    \/\/ \u5c06 Less \u7ed1\u5b9a\u5230 ByWeight \u4e0a\n\nfunc main() {\n    s := []Person{\n        {\"apple\", 12},\n        {\"pear\", 20},\n        {\"banana\", 50},\n        {\"orange\", 87},\n        {\"hello\", 34},\n        {\"world\", 43},\n    }\n\n    sort.Sort(ByWeight{s})\n    fmt.Println(\"People by weight:\")\n    printPeople(s)\n\n    sort.Sort(ByName{s})\n    fmt.Println(\"\\nPeople by name:\")\n    printPeople(s)\n\n}\n\nfunc printPeople(s []Person) {\n    for _, o := range s {\n        fmt.Printf(\"%-8s (%v)\\n\", o.Name, o.Weight)\n    }\n}\n<\/code><\/pre>\n<p>\u5bf9\u7ed3\u6784\u4f53\u7684\u6392\u5e8f\uff0c \u6682\u65f6\u5c31\u5230\u8fd9\u91cc\u3002 \u7b2c\u4e00\u79cd\u6392\u5e8f\u5bf9\u53ea\u6839\u636e\u4e00\u4e2a\u5b57\u6bb5\u7684\u6bd4\u8f83\u5408\u9002\uff0c \u53e6\u5916\u4e09\u4e2a\u662f\u9488\u5bf9\u53ef\u80fd\u6839\u636e\u591a\u4e2a\u5b57\u6bb5\u6392\u5e8f\u7684\u3002\u65b9\u6cd5 4 \u6211\u8ba4\u4e3a\u6bcf\u6b21\u90fd\u8981\u591a\u6784\u9020\u4e00\u4e2a ByXXX \uff0c \u9887\u4e3a\u4e0d\u4fbf\uff0c \u8fd9\u6837\u591a\u9ebb\u70e6\uff0c\u4e0d\u5982\u65b9\u6cd5 2 \u548c\u65b9\u6cd5 3 \u6765\u7684\u65b9\u4fbf\uff0c\u76f4\u63a5\u4f20\u8fdb\u53bb\u4e00\u4e2a cmp\u3002 \u65b9\u6cd52\u3001 3 \u6ca1\u6709\u592a\u5927\u7684\u5dee\u522b\uff0c 3 \u53ea\u662f\u7b80\u5355\u5c01\u88c5\u4e86\u4e00\u4e0b\u800c\u5df2\uff0c \u5bf9\u4e8e\u4f7f\u7528\u8005\u6765\u8bf4\uff0c \u53ef\u80fd\u4f1a\u66f4\u65b9\u4fbf\u4e00\u4e9b\uff0c\u800c\u4e14\u4e5f\u4f1a\u66f4\u5c11\u7684\u51fa\u9519\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010Go\u8bed\u8a00\u3011\u57fa\u672c\u7c7b\u578b\u6392\u5e8f\u548c slice \u6392\u5e8f Go \u662f\u901a\u8fc7 sort \u5305\u63d0\u4f9b\u6392\u5e8f\u548c\u641c\u7d22\uff0c\u56e0\u4e3a Go \u6682\u65f6\u4e0d\u652f\u6301 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[398],"class_list":["post-1253","post","type-post","status-publish","format-standard","hentry","category-15","tag-go"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1253"}],"version-history":[{"count":6,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1253\/revisions"}],"predecessor-version":[{"id":1517,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1253\/revisions\/1517"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}