In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. Go language allows you to sort the elements of the slice according to its type. So, a string type slice is sorted by using the following functions. These functions are defined under the sort package so, you have to import sort package in your program for accessing these functions:
1. Strings: This function is used to only sorts a slice of strings and it sorts the elements of the slice in increasing order.
func Strings(scl string)
Here, slc represent a slice of strings. Let us discuss this concept with the help of an example:
Slices(Before): Slice 1: [abc rwp def por ber erj] Slice 2: [Rabbit Fish Dog Parrot Cat Hamster] Slices(After): Slice 1 : [abc ber def erj por rwp] Slice 2 : [Cat Dog Fish Hamster Parrot Rabbit]
2. StringsAreSorted: This function is used to check whether the given slice of strings is in sorted form(in increasing order) or not. This method returns true if the slice is in sorted form, or return false if the slice is not in the sorted form.
func StringsAreSorted(scl string) bool
Here, scl represents a slice of strings. Let us discuss this concept with the help of an example:
Slices: Slice 1: [abc ber def erj por rwp] Slice 2: [Rabbit Fish Dog Parrot Cat Hamster] Result: Is Slice 1 is sorted?: true Is Slice 2 is sorted?: false
- How to copy one slice into another slice in Golang?
- How to sort a slice of ints in Golang?
- How to sort a slice of float64s in Golang?
- How to sort a slice stable in Golang?
- How to sort a slice in Golang?
- How to sort a slice of Search in Golang?
- How to convert a slice of bytes in uppercase in Golang?
- How to convert a slice of bytes in lowercase in Golang?
- Check if the given slice is sorted in Golang
- Searching an element of float64 type in Golang slice
- How to pass a Slice to Function in Golang?
- Check if the specified element is present in the slice of bytes in Golang
- How to count specific characters present in the slice in Golang?
- Checking Slice of bytes for equality under Unicode case folding in Golang
- Check if the Slice of bytes starts with specified prefix in Golang
- Check if the Slice of bytes ends with specified suffix in Golang
- How to join the elements of the byte slice in Golang?
- How to replace a specified element in slice of bytes in Golang?
- How to replace all the elements in slice of bytes in Golang?
- How to repeat a slice of bytes in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.