https:\/\/golang.org\/pkg\/sort\/#Interface<\/a><\/p>\n\n\n\ntype Interface interface {\n \/\/ Len is the number of elements in the collection.\n Len() int\n \n \/\/ Less reports whether the element with\n \/\/ index i should sort before the element with index j.\n Less(i, j int) bool\n \n \/\/ Swap swaps the elements with indexes i and j.\n Swap(i, j int)\n}<\/code><\/pre>\n\n\n\nLet’s see a working example to illustrate how we can use sort.Interface<\/strong> to sort a user-defined struct. In below example <\/p>\n\n\n\nWe created a custom struct called employee<\/strong> with name<\/strong> and salary<\/strong> in dollars as fields<\/li><\/ul>\n\n\n\nWe have a employeeList<\/strong> which hold the list of employee<\/strong>.<\/li><\/ul>\n\n\n\nemployeeList<\/strong> implements the Len(), Less(), Swap()<\/strong> method hence it implements the sort.Interface<\/strong><\/li><\/ul>\n\n\n\nWe sort the employee from highest salary to lowest salary. To sort employeeList we pass it to sort.Sort()<\/strong> function<\/li><\/ul>\n\n\n\n<\/p>\n\n\n\n
<\/span>Full Working Code:<\/strong><\/span><\/h1>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"sort\"\n)\n\ntype employee struct {\n name string\n salary int\n}\n\ntype employeeList []employee\n\nfunc (e employeeList) Len() int {\n return len(e)\n}\n\nfunc (e employeeList) Less(i, j int) bool {\n return e[i].salary > e[j].salary\n}\n\nfunc (e employeeList) Swap(i, j int) {\n e[i], e[j] = e[j], e[i]\n}\n\nfunc main() {\n eList := []employee{\n employee{name: \"John\", salary: 3000},\n employee{name: \"Bill\", salary: 4000},\n employee{name: \"Sam\", salary: 1000},\n }\n sort.Sort(employeeList(eList))\n for _, employee := range eList {\n fmt.Printf(\"Name: %s Salary %d\\n\", employee.name, employee.salary)\n }\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nName: Bill Salary 4000\nName: John Salary 3000\nName: Sam Salary 1000<\/code><\/pre>\n\n\n\nTo sort from lowest salary to highest salary we need to change the Less<\/strong> function with ‘>’<\/strong> sign<\/p>\n\n\n\nfunc (e employeeList) Less(i, j int) bool {\n return e[i].salary > e[j].salary\n}<\/code><\/pre>\n\n\n\nAfter changing it when we run the program then output will be:<\/p>\n\n\n\n
Name: Sam Salary 1000\nName: John Salary 3000\nName: Bill Salary 4000<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents IntroductionFull Working Code: Introduction GO has a sort package that provides utility primitives for the sorting of slices and user-defined types. Any collection can be sorted by the Sort…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[45,125,3,124],"class_list":["post-1009","post","type-post","status-publish","format-standard","hentry","category-tech","tag-array","tag-custom","tag-go","tag-structs"],"yoast_head":"\n
Sort a Custom Struct in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n