{"id":7817,"date":"2023-05-17T10:09:40","date_gmt":"2023-05-17T10:09:40","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=7817"},"modified":"2023-07-26T08:40:55","modified_gmt":"2023-07-26T08:40:55","slug":"sort-slice-int-reverse-in-golang","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/golang\/sort-slice-int-reverse-in-golang","title":{"rendered":"Golang Sort Int Reverse"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">keys := []int{3, 2, 8, 1}\r\n    sort.Ints(keys)\r\n    for i, j := 0, len(keys)-1; i &lt; j; i, j = i+1, j-1 {\r\n        keys[i], keys[j] = keys[j], keys[i]\r\n    }\r\n    fmt.Println(keys)\r\n<\/pre>\n<p>This Go code sorts a slice of integers in descending order using the built-in sort.Ints function and a reverse iteration. Here&#8217;s how it works:<\/p>\n<p>1. First, a slice of integers named keys is defined and initialized with the values {3, 2, 8, 1}.<br \/>\n2. Then, the sort.Ints function is called with keys as an argument. This function sorts the slice in ascending order.<br \/>\n3. After the sort function has been called, the slice keys will be in the order {1, 2, 3, 8}.<br \/>\n4. Next, a for loop is used to iterate over the slice in reverse order. The loop starts with i set to 0, j set to the length of the slice minus 1 (3 in this case), and the condition i < j. In each iteration, i is incremented by 1, and j is decremented by 1.\n5. Inside the loop, the values at positions i and j in the slice are swapped using a multiple-assignment statement: keys[i], keys[j] = keys[j], keys[i]. This effectively reverses the order of the slice.\n6. After the loop completes, the slice keys will be in the order {8, 3, 2, 1}.\n7. Finally, the fmt.Println function is called with keys as an argument to print the contents of the slice to the console. The output will be [8 3 2 1].\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>keys := []int{3, 2, 8, 1} sort.Ints(keys) for i, j := 0, len(keys)-1; i &lt; j; i, j = i+1, j-1 { keys[i], keys[j] = keys[j], keys[i] } fmt.Println(keys) This Go code sorts a slice of integers in descending order using the built-in sort.Ints function and a reverse iteration. Here&#8217;s how it works: 1. First, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8306,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-7817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7817"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=7817"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7817\/revisions"}],"predecessor-version":[{"id":8307,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7817\/revisions\/8307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8306"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=7817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=7817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=7817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}