{"id":1190,"date":"2022-07-20T21:42:06","date_gmt":"2022-07-20T13:42:06","guid":{"rendered":"https:\/\/www.mustenaka.cn\/?p=1190"},"modified":"2023-03-14T11:14:42","modified_gmt":"2023-03-14T03:14:42","slug":"simplifiedversionofgolangsort-go-simplesort","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2022\/07\/20\/simplifiedversionofgolangsort-go-simplesort\/","title":{"rendered":"\u6539\u8fdb\u7684Golang\u6392\u5e8f\u5e93-Golang\u7b80\u5355\u6392\u5e8f"},"content":{"rendered":"<p>\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/Mustenaka\/Go-simpleSort\">https:\/\/github.com\/Mustenaka\/Go-simpleSort<\/a><\/p>\n<h1>\u6539\u8fdb\u7684Golang\u6392\u5e8f\u5de5\u5177<\/h1>\n<p>\u672c\u5de5\u7a0b\u4e3b\u8981\u662fgo\u57281.18 Bate1\u7248\u672c\u4e4b\u524d\u6ca1\u6709\u5f15\u5165\u8303\u578b\u6982\u5ff5\uff0c\u6bcf\u4e00\u6b21\u4f7f\u7528Sort\u6392\u5e8f\u90fd\u8981\u58f0\u660e\u4e00\u5806\u4e1c\u897f\uff0c\u60f3\u8981\u5b9e\u73b0\u7c7b\u4f3c\u5176\u4ed6\u8bed\u8a00\u90a3\u6837\u7684\u4efb\u610f\u6392\u5e8f\u800c\u6784\u4ef6\u7684\uff0c\u901a\u8fc7\u53cd\u5c04\u5b9e\u73b0\u4e86\u81ea\u5b9a\u4e49\u8f93\u5165\u7684\u6570\u636e\uff0c\u7136\u540e\u58f0\u660e\u9700\u8981\u6392\u5e8f\u7684\u8d44\u6e90\uff0c\u8fdb\u884c\u81ea\u52a8\u6392\u5e8f\uff0c\u968f\u7740Go\u7684\u7248\u672c\u9010\u6b65\u6df1\u5165<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" null=\"\">package simplesort\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"reflect\"\r\n\t\"sort\"\r\n\r\n\t\"github.com\/Mustenaka\/Go-simpleSort\/sortConfig\"\r\n)\r\n\r\ntype SortBy []interface{}\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\/\/ Get Sort index\r\n\tsortIndex := sortConfig.GetInstance().GetIndex()\r\n\r\n\t\/\/ \u5b9a\u4f4d\u6bd4\u8f83\u5b57\u6bb5\r\n\tfieldValue1 := reflect.ValueOf(a[i]).Field(sortIndex)\r\n\tfieldValue2 := reflect.ValueOf(a[j]).Field(sortIndex)\r\n\r\n\t\/\/ \u65ad\u8a00\u7c7b\u578b\r\n\tswitch fieldValue1.Kind() {\r\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\r\n\t\treturn fieldValue1.Int() &lt; fieldValue2.Int()\r\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\r\n\t\treturn fieldValue1.Uint() &lt; fieldValue2.Uint()\r\n\tcase reflect.Float32, reflect.Float64:\r\n\t\treturn fieldValue1.Float() &lt; fieldValue2.Float()\r\n\tcase reflect.String:\r\n\t\treturn fieldValue1.String() &lt; fieldValue2.String()\r\n\tdefault:\r\n\t\tpanic(\"unsupported kind\")\r\n\t}\r\n}\r\n\r\n\/\/ @title Simplesort\r\n\/\/ @description simplified non-generic sort method.\r\n\/\/ @param data interface{} \"Data to be sorted.\"\r\n\/\/ @param filedName string \"sorted index field name.\"\r\n\/\/ @param order bool \"oder or reverse order\"\r\n\/\/ @return interface{}, error \"sort results, nil if no error.\"\r\nfunc Simplesort(args interface{}, filedName string, order bool) ([]interface{}, error) {\r\n\t\/\/ \u8f93\u51fa\u6570\u636e\r\n\tfmt.Println(\"input: \", args)\r\n\r\n\t\/\/ interface{} convert to []interface{}\r\n\tvar interfaceSlice []interface{}\r\n\r\n\t\/\/ \u5224\u65ad\u7c7b\u578b\r\n\tif reflect.TypeOf(args).Kind() != reflect.Slice {\r\n\t\tpanic(\"input need slice kind\")\r\n\t}\r\n\r\n\t\/\/ \u8f93\u51fa\u7c7b\u578b\u4fe1\u606f\r\n\tfmt.Println(\"args typeof kind: \" + reflect.TypeOf(args).Kind().String())\r\n\r\n\t\/\/ \u5b9a\u4f4d\u76ee\u6807\u5b57\u6bb5\u5728\u7ed3\u6784\u4f53\u4e2d\u7684\u7d22\u5f15\r\n\tvar index int = 0\r\n\r\n\t\/\/ \u5207\u7247\u5904\u7406\r\n\ts := reflect.ValueOf(args)\r\n\tfor i := 0; i &lt; s.Len(); i++ {\r\n\t\tele := s.Index(i)\r\n\t\tt := ele.Type()\r\n\r\n\t\tinterfaceSlice = append(interfaceSlice, ele.Interface())\r\n\r\n\t\t\/\/ \u68c0\u67e5\u9700\u8981\u6392\u5e8f\u5b57\u6bb5\u662f\u5426\u5305\u542b\u5728\u7ed3\u6784\u4f53\u4e2d\r\n\t\tvar isExist bool = false\r\n\t\tfor ii := 0; ii &lt; ele.NumField(); ii++ {\r\n\t\t\t\/\/ fmt.Printf(\"name: %s, type: %s, value: %v\\n\",\r\n\t\t\t\/\/ \tt.Field(ii).Name,\r\n\t\t\t\/\/ \tele.Field(ii).Type(),\r\n\t\t\t\/\/ \tele.Field(ii))\r\n\t\t\tif t.Field(ii).Name == filedName {\r\n\t\t\t\tisExist = true\r\n\t\t\t\tindex = ii\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\/\/ \u672a\u627e\u5230\u9700\u8981\u6392\u5e8f\u7684\u5b57\u6bb5\uff0c\u629b\u51fa\u9519\u8bef\r\n\t\tif !isExist {\r\n\t\t\t\/\/ panic(\"not found filed: \" + filedName)\r\n\t\t\treturn nil, fmt.Errorf(\"not found filed: %s\", filedName)\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/ \u5904\u7406\u5b8c\u6210\uff0c\u521d\u59cb\u5316\u6392\u5e8f\u7684\u914d\u7f6e - \u7528\u5355\u4f8b\u7684\u65b9\u5f0f\u6ce8\u5165Less\u65b9\u6cd5\r\n\tsortConfig.CreateInstance(index, filedName, order)\r\n\r\n\t\/\/ \u5bf9\u7ed3\u6784\u4f53\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\r\n\tsort.Sort(SortBy(interfaceSlice))\r\n\r\n\t\/\/ \u8f93\u51fa\u6570\u636e\r\n\tfmt.Println(\"output: \", interfaceSlice)\r\n\treturn interfaceSlice, nil\r\n}\r\n\r\n\/\/ @title SimplesortStable\r\n\/\/ @description reflect test\r\nfunc FunctionTest(data interface{}) {\r\n\tgetValue := reflect.ValueOf(data) \/\/ Value of v\r\n\tif getValue.Kind() != reflect.Slice {\r\n\t\tpanic(\"need slice kind\")\r\n\t}\r\n\r\n\tl := getValue.Len()\r\n\tfor i := 0; i &lt; l; i++ {\r\n\t\tvalue := getValue.Index(i) \/\/ Value of item\r\n\t\ttypel := value.Type()      \/\/ Type of item\r\n\t\tif typel.Kind() != reflect.Struct {\r\n\t\t\tpanic(\"need struct kind\")\r\n\t\t}\r\n\r\n\t\tfmt.Printf(\"type-kind: %s, type-name: %s, value: %v\\n\", typel.Kind(), typel.Name(), value.Interface())\r\n\r\n\t\tnum := value.NumField()\r\n\t\tfor j := 0; j &lt; num; j++ {\r\n\t\t\tfmt.Printf(\"name: %s, type: %s, value: %v\\n\",\r\n\t\t\t\ttypel.Field(j).Name,\r\n\t\t\t\tvalue.Field(j).Type(),\r\n\t\t\t\tvalue.Field(j))\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\/\/ \u5355\u4f8b\u6a21\u5f0f\u6d4b\u8bd5\r\nfunc SinTest(filedName string) {\r\n\tsortConfig.CreateInstance(2, filedName, true)\r\n\r\n\tfmt.Println(\"instance: \", sortConfig.GetInstance().GetIndex())\r\n\tfmt.Println(\"instance: \", sortConfig.GetInstance().GetName())\r\n\tfmt.Println(\"instance: \", sortConfig.GetInstance().GetOrder())\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/Mustenaka\/Go-simpleSort \u6539\u8fdb [&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-1190","post","type-post","status-publish","format-standard","hentry","category-go","tag-golang"],"views":711,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1190","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=1190"}],"version-history":[{"count":5,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":1321,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions\/1321"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}