Slice Sort
Slice sorting allows you to interact with and manipulate slices in various ways. With the help of Golang sort functions, we can search any list of critical Golang functions. We need to import the "sort" package at the beginning of the code to implement the sort.
Ints function [ascending order]
This Ints function sorts the slice of integers in ascending order.
Syntax
func Ints(intSlice []int)
Example
package main
import (
"fmt"
"sort"
)
func main() {
intSlice := []int{10, 5, 25, 351, 14, 9}
fmt.Println("Slice of integer before the sort:",intSlice)
sort.Ints(intSlice)
fmt.Println("Slice of integer after the sort:",intSlice)
}
Output
Slice of integer before the sort: [10 5 25 351 14 9]
Slice of integer after the sort: [5 9 10 14 25 351]
In this example, we have used the slice sort function. In this, we have taken a list of integers and sorted them in ascending order.
Strings function [ascending order]
The String function sorts a slice of strings in ascending order.
Syntax
func Strings(strSlice [] string)
Example
package main
import (
"fmt"
"sort"
)
func main() {
strSlice := []string{"Coding","Ninjas","Vertical","Blogs"}
fmt.Println("Slice of string BEFORE sort:",strSlice)
sort.Strings(strSlice)
fmt.Println("Slice of string AFTER sort:",strSlice)
}
Output
Slice of string BEFORE sort: [Coding Ninjas Vertical Blogs]
Slice of string AFTER sort: [Blogs Coding Ninjas Vertical]
Reverse function [descending order]
The Reverse function returns a slice in the reverse order.
Syntax
func Reverse(data Interface) Interface
Example
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{17, 5, 34, 55, 531, 98, 7, 12, 15, 105}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
fmt.Println("\n",a)
a = []int{-17, -5, -34, -55, -531, -98, -7, -12, -15, -105}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
fmt.Println("\n",a)
b := []string{"Coding","Blogs","Ninjas","Vertical","Data","Structure"," New Delhi"}
sort.Sort(sort.Reverse(sort.StringSlice(b)))
fmt.Println("\n",b)
b = []string{"Blogs","New Delhi","vertical","blogs","Structure","ninjas","Coding"}
sort.Sort(sort.Reverse(sort.StringSlice(b)))
fmt.Println("\n",b)
c := []float64{70.10, 50.10, 140.15, 80.15, 6.95}
sort.Sort(sort.Reverse(sort.Float64Slice(c)))
fmt.Println("\n",c)
c = []float64{-70.10, -50.10, -140.15, -80.15, -6.95}
sort.Sort(sort.Reverse(sort.Float64Slice(c)))
fmt.Println("\n",c)
}
Output
[531 105 98 55 34 17 15 12 7 5]
[-5 -7 -12 -15 -17 -34 -55 -98 -105 -531]
[Vertical Structure Ninjas Data Coding Blogs New Delhi]
[vertical ninjas blogs Structure New Delhi Coding Blogs]
[140.15 80.15 70.1 50.1 6.95]
[-6.95 -50.1 -70.1 -80.15 -140.15]
We have taken several inputs in this example: a string and integer. We sorted them in descending order and then showed the output of it.
Also Read - Selection Sort in C
SearchInts function [ascending order]
The SearchInts function will search the position of x in a sorted slice of integer and returns the index as specified by Search. This function works only if the slice is in the sort order only.
Syntax
func SearchInts(intSlice []int, x int) int
Example
package main
import (
"fmt"
"sort"
)
func main() {
intSlice := []int{45, 12, 28, 19, 14, 88, 28, 16, 55, 65}
x := 16
pos := sort.SearchInts(intSlice,x)
fmt.Printf("Present %d at index %d in %v\n", x, pos, intSlice)
sort.Ints(intSlice)
pos = sort.SearchInts(intSlice,x)
fmt.Printf("Present %d at index %d in %v\n", x, pos, intSlice)
x = 65
pos = sort.SearchInts(intSlice,x)
fmt.Printf("Present %d at index %d in %v\n", x, pos, intSlice)
x = 88
pos = sort.SearchInts(intSlice,x)
fmt.Printf("Present %d at index %d in %v\n", x, pos, intSlice)
x = -5
pos = sort.SearchInts(intSlice,x)
fmt.Printf("Present %d at index %d in %v\n", x, pos, intSlice)
}
Output
Present 16 at index 2 in [45 12 28 19 14 88 28 16 55 65]
Present 16 at index 2 in [12 14 16 19 28 28 45 55 65 88]
Present 65 at index 8 in [12 14 16 19 28 28 45 55 65 88]
Present 88 at index 9 in [12 14 16 19 28 28 45 55 65 88]
Present -5 at index 0 in [12 14 16 19 28 28 45 55 65 88]
In this example, we have taken an array to check whether a particular integer occurs or not. We also used the search function to check whether the number exists or not otherwise printed the suitable message.
FAQs
-
How to sort a slice in the Go language?
In Go lang, you can sort the elements present in the slice. The standard library contains a sort package with different sorting methods to silt the slice.
-
How to sort data in the Go lang?
In Go lang, there are mainly three ways to sort data. These are Slice of ints, float64s, or strings custom comparator.
-
How to sort a slice of data in an interface?
To sort data in an interface, an appropriate interface method provider, e.g., StringSlice, IntSlice, or Float64s, and sort.
Key Takeaways
This article covered the Go slice, sort, reverse, and search functions in this article. We briefly introduced these topics. We also discussed Go Sort, Slice Sort, and different functions. We also discussed different types of functions along with their examples. I hope you might have found this article helpful. If you want to learn more articles, please visit Java Articles. If you are going to practice DSA problems, please visit Contests and practice to improve your skills.
Related articles:
Happy Coding!