- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to sort a string
A string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type. In this article, we will learn different techniques to sort a string using different set of examples.
Syntax
func Split(str, sep string) []string
Split() function is used to split a string through a provided separator. This function is present in strings package and it accepts the string to split as an argument along with a separator. The function then returns the final array of strings as a result.
func Strings(src []string) []string
The strings function is defined in sort package. This function takes the array in string format that we wish to sort and returns the result by sorting that array.
Algorithm
Step 1 − Create a package main and declare fmt(format package) ,sort and strings package
Step 2 − Star the main function
Step 3 − Split the string into single characters
Step 4 − Using the internal function to sort the Characters and combine them in string
Step 5 − Print the output
Example1
In this example, we will use sort.Slice function to sort the unsorted string. The output printed on the console will be a sorted string. Let’s see through the code and the algorithm to know how to execute this program.
package main import ( "fmt" "sort" //import fmt and sort package ) //create main function to execute the program func main() { unsorted_str := "ghfisaw" //create unsorted string fmt.Println("The unsorted string is:", unsorted_str) chars := []rune(unsorted_str) sort.Slice(chars, func(i, j int) bool { //sort the string using the function return chars[i] < chars[j] }) fmt.Println("The sorted string is:") fmt.Println(string(chars)) //print the string on the console }
Output
The unsorted string is: ghfisaw The sorted string is: afghisw
Example 2
In this example, we will see how to sort a string using sort.Slice function. Here, the unsorted string will be converted to rune and it is passed as input in the sort.Slice function to sort the string.
package main import ( "fmt" "sort" //import fmt and sort package ) //create a main function to execute the program func main() { unsorted_str := "dbl" //create an unsorted string fmt.Println("The unsorted string created here is:", unsorted_str) strrunes := []rune(unsorted_str) //convert the string to rune sort.Slice(strrunes, func(i, j int) bool { return strrunes[i] < strrunes[j] //sort the string rune }) fmt.Println("The sorted string is:") fmt.Println(string(strrunes)) //print sorted string }
Output
The unsorted string created here is: dbl The sorted string is: bdl
Example 3
In this example we will sort the string using strings.split function and sort.Strings. The former is used to split the characters and the latter is used to sort the characters. The output will be printed on the console using fmt.Println() function.
package main import ( "fmt" "sort" "strings" //import fmt, sort and strings package ) //create a main function to execute the program func main() { unsorted_str := "kertgld" //create an unsorted string fmt.Println("The unsorted string created here is:", unsorted_str) chars := strings.Split(unsorted_str, "") //split the string into characters sort.Strings(chars) //sort the characters fmt.Println("The sorted string formed here is:") fmt.Println(strings.Join(chars, "")) //join the characters and print on console }
Output
The unsorted string created here is: kertgld The sorted string formed here is: degklrt
Conclusion
We executed the program of sorting a string using three examples respectively. In the first example we used sort.Slice() function to sort the string, in the second example we converted unsorted string to rune and then applied sort.Slice() function on it and in the third example we used sort.strings and strings.split function where string is splitted and then sorted based on characters. Hence, program executed successfully.