Introduction to Golang Sort
In Go language sorting allow us to sort numeric or string of array in the format we want , for example if we have list of students with marks of the students and we wanted to sort the student list according to the roll number or according to the marks of the students then we can use the sort , sort contains various methods like methods to check if the given array of the students are sorted or not by returning the true and false value ,biggest benefit of sort in the go language it can sort upto each letter of the string with writing the minimum code.
Syntax
See the below syntax for understanding,
sort.sort_function_name(array_of_list)
In the above we have written a simple syntax for the go sort, we can explain each attribute of the below syntax,
- sort: The sort is a package in go language which we need to import in our program to use the available features of the sorting.
- sort_function_name: These attributes are the functions of the sort package of the go language, for example if it has a function to check if the array is sorted or not, we have the function sort array according to alphabet cases.
- array_of_lis: This is the list of the array on which we are going to perform our operation, for example, if we want to sort string then we can use the array of the string students and after sorting the output with sorted array will be there.
How does Sorting work in the Go language?
Before understanding the working of the sort in go language let us understand the uses , suppose we have huge list of the array or slice and data contains the information of the students with marks and registration number of the students and we want get the new array or slice in the sorted form either it can be sorted by marks or registration number for easy visibility of the data ..Working of the sorting in the go language is very simple we can explain in some important points,
- First of all before using the features of the sort in the go language we need to import the main package of the sort .
- On the sort we can call the functions available in the go language and these functions will get the array or slice .
- Sorting operations can also perform on the slice other than array with help of the method slice .
- Once we use the sort it will expect its function for performing its task on the array or slice .We will discuss more in example .
Sorting Methods in Go
Below are the methods of sorting in go Language:
1. String
This method we used to sort the string containing arrays.
Example,
Let us discuss little about the below example ,
- Here we have taken two arrays and they are not sorted initially .
- First we printed the unsorted array .
- Then we sorted them as the array contains the string name so we used the method String on sort .And finally the output was sorted as an array of students .
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"sort"
)
func main() {
Human := []string{"Ranjan", "Ajay", "Vijay", "Sujit", "Vikash", "Akash"}
Animals := []string{"Cow", "Lion", "Tiger",
"Cat", "Rabbit", "Panda"}
fmt.Println("Printing the value of the human and animal list")
fmt.Println("Human list before: ", Human)
fmt.Println("Animal list before: ", Animals)
sort.Strings(Human)
sort.Strings(Animals)
fmt.Println("Printing the Human and animals after sorting:")
fmt.Println("Human list After: ", Human)
fmt.Println("Animal list before: ", Animals)
}
Output:
2. StringArraySorted
This method is used to check if the given array is sorted or not, it will take an array of the data and return the output as the true and false.
Example,
Let be give little brief about the below ,
- In the below example we have taken two arrays, one of human and another with animals array.
- First we printed then and then we used the methodStringArraySorted and it returns the output in the form of boolean values like true and false on the basis of if the array is sorted or not .
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"sort"
)
func main() {
Human := []string{"Ranjan", "Ajay", "Vijay", "Sujit", "Vikash", "Akash"}
Animals := []string{"Cow", "Lion", "Tiger",
"Cat", "Rabbit", "Panda"}
fmt.Println("Printing the value of the human and animal list")
fmt.Println("Human list before: ", Human)
fmt.Println("Animal list before: ", Animals)
check1 := sort.StringsAreSorted(Human)
check2 := sort.StringsAreSorted(Animals)
fmt.Println("Printing the Human and animals after sorting:")
fmt.Println("Checking if sorted: ", check1)
fmt.Println("Checking if sorted", check2)
}
Output:
3. Slice
This is the method used over sort method. We can use this method for any slice (a slice is the data of storing ), we can explain the below example.
Example,
- We have created a slice of students with the student name ,marks, and id of the students.
- In the example, we are sorting the slice on the basis of the name (string),marks, and id of the students and printing the output of their sorting.
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"sort"
)
func main() {
Student := []struct {
s_name string
s_marks int
s_id int
}{
{"Ranjan", 280, 1098},
{"Ajay", 280, 300},
{"Anita", 330, 109},
{"Kavita", 100, 107},
{"Ashima", 444, 258},
{"Rohit", 450, 188},
{"Vijay", 289, 118},
{"Dhanush", 239, 329},
{"Priya", 400, 123},
{"Nikita", 312, 111},
}
sort.Slice(Student, func(i, j int) bool {
return Student[i].s_name < Student[j].s_name })
fmt.Println("Print the out of sorted student according to name:")
fmt.Println(Student)
sort.Slice(Student, func(i, j int) bool {
return Student[i].s_marks < Student[j].s_marks })
fmt.Println()
fmt.Println("Print the out of sorted student according to marks:")
fmt.Println(Student)
sort.Slice(Student, func(i, j int) bool {
return Student[i].s_id < Student[j].s_id })
fmt.Println()
fmt.Println("Print Students after sorting by their id:")
fmt.Println(Student)
}
Output:
Conclusion – Golang Sort
From this tutorial we learned about the basics of sort and its working in the go language, we also looked upon the syntax of the sort in the go language. We focus on some of the important examples and with the function of the sort in go language.
Recommended Articles
This is a guide to Golang Sort. Here we discuss an introduction to Golang Sort, syntax, and three different methods with examples to implement. You can also go through our other related articles to learn more –