{"id":6210,"date":"2021-08-21T01:22:29","date_gmt":"2021-08-20T19:52:29","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=6210"},"modified":"2021-08-21T01:23:08","modified_gmt":"2021-08-20T19:53:08","slug":"sort-part-slice-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/","title":{"rendered":"Sort a part of the slice in Go (Golang)"},"content":{"rendered":"\n

Overview<\/strong><\/p>\n\n\n\n

sort.Slice<\/strong> package of golang can be used to sort a full slice or a part of the sliceFull slice sorting in asc order<\/p>\n\n\n\n

package main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n)\n\nfunc main() {\n\n\tnums := []int{3, 4, 2, 1}\n\tsort.Slice(nums, func(i, j int) bool {\n\t\treturn nums[i] < nums[j]\n\t})\n\tfmt.Println(nums)\n}<\/code><\/pre>\n\n\n\n

Output<\/strong><\/p>\n\n\n\n

[1,2,3,4]<\/code><\/pre>\n\n\n\n

Full slice sorting in desc order<\/p>\n\n\n\n

package main\nimport (\n    \"fmt\"\n    \"sort\"\n)\nfunc main() {\n    nums := []int{3, 4, 2, 1}\n    sort.Slice(nums, func(i, j int) bool {\n        return nums[i] > nums[j]\n    })\n    fmt.Println(nums)\n}<\/code><\/pre>\n\n\n\n

Output<\/strong><\/p>\n\n\n\n

[4 3 2 1]<\/code><\/pre>\n\n\n\n

Part slice sorting<\/p>\n\n\n\n

package main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n)\n\nfunc main() {\n\n\tnums := []int{3, 4, 2, 1}\n\tstart := 2\n\tend := 4\n\tsort.Slice(nums[start:end], func(i, j int) bool {\n\t\treturn nums[start+i] < nums[start+j]\n\t})\n\tfmt.Println(nums)\n}<\/code><\/pre>\n\n\n\n

Output<\/strong><\/p>\n\n\n\n

[3 4 1 2]<\/code><\/pre>\n\n\n\n

Notice that it has sorted the last two elements in the asc order.<\/p>\n\n\n\n

Note: <\/strong>Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial<\/a><\/p>\n\n\n\n

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you -All Design Patterns Golang<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Overview sort.Slice package of golang can be used to sort a full slice or a part of the sliceFull slice sorting in asc order Output Full slice sorting in desc order Output…<\/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":[3,4],"class_list":["post-6210","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\nSort a part of the slice in Go (Golang) - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sort a part of the slice in Go (Golang) - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"Overview sort.Slice package of golang can be used to sort a full slice or a part of the sliceFull slice sorting in asc order Output Full slice sorting in desc order Output...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-20T19:52:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-20T19:53:08+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/\",\"url\":\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/\",\"name\":\"Sort a part of the slice in Go (Golang) - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2021-08-20T19:52:29+00:00\",\"dateModified\":\"2021-08-20T19:53:08+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/sort-part-slice-go\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sort a part of the slice in Go (Golang)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sort a part of the slice in Go (Golang) - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/","og_locale":"en_US","og_type":"article","og_title":"Sort a part of the slice in Go (Golang) - Welcome To Golang By Example","og_description":"Overview sort.Slice package of golang can be used to sort a full slice or a part of the sliceFull slice sorting in asc order Output Full slice sorting in desc order Output...","og_url":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2021-08-20T19:52:29+00:00","article_modified_time":"2021-08-20T19:53:08+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/","url":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/","name":"Sort a part of the slice in Go (Golang) - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2021-08-20T19:52:29+00:00","dateModified":"2021-08-20T19:53:08+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/sort-part-slice-go\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/sort-part-slice-go\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Sort a part of the slice in Go (Golang)"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-1Ca","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/6210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=6210"}],"version-history":[{"count":2,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/6210\/revisions"}],"predecessor-version":[{"id":6213,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/6210\/revisions\/6213"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=6210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=6210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=6210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}