Home >Backend Development >Golang >Let's talk about how to use the Golang sort package
Golang is an efficient, simple and reliable programming language that is widely used in server-side development and system programming. In Golang, the sort package provides a rich sorting function that can meet various sorting needs. This article will introduce how to use the Golang sort package.
The sort package provides functions for sorting on different types of sequences, such as []int, []float64, []string, etc. It also provides a general sorting interface sort.Interface, which can be used to define custom types of sorting. The sorting algorithms provided by the sort package are some optimized quick sort and heap sort. In the sort package, there are three main functions: Sort, Reverse and IsSorted.
Sort function sorts a sequence that implements sort.Interface in ascending order. The sort.Interface interface defines three methods: Len, Swap and Less. Among them, the Len method returns the length of the sequence, the Swap method exchanges the positions of two elements, and the Less method returns whether the element at position i is smaller than the element at position j. An example is as follows:
package main import ( "fmt" "sort" ) type persons []struct { name string age int } func (ps persons) Len() int { return len(ps) } func (ps persons) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] } func (ps persons) Less(i, j int) bool { return ps[i].age < ps[j].age } func main() { ps := persons{{"Tom", 25}, {"Jerry", 20}, {"Alice", 30}} sort.Sort(ps) fmt.Println(ps) }
The output result is:
[{Jerry 20} {Tom 25} {Alice 30}]
The Reverse function returns a reverse sequence of a sequence that implements the sort.Interface interface. The example is as follows:
package main import ( "fmt" "sort" ) func main() { ns := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5} sort.Sort(sort.Reverse(sort.IntSlice(ns))) fmt.Println(ns) }
The output result is:
[9 6 5 5 5 4 3 3 2 1 1]
The IsSorted function determines whether a sequence that implements sort.Interface has followed the Less method The rules are sorted. An example is as follows:
package main import ( "fmt" "sort" ) func main() { ns := []int{1, 2, 3, 3, 4, 5} fmt.Println(sort.IsSorted(sort.IntSlice(ns))) ns = []int{1, 2, 3, 4, 3, 5} fmt.Println(sort.IsSorted(sort.IntSlice(ns))) }
The output result is:
true false
We can also sort based on specific attributes of custom types. The example is as follows:
package main import ( "fmt" "sort" ) type Person struct { Name string Age int } type Persons []*Person func (ps Persons) Len() int { return len(ps) } func (ps Persons) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] } func (ps Persons) Less(i, j int) bool { return ps[i].Age < ps[j].Age } func main() { ps := Persons{{"Tom", 25}, {"Jerry", 20}, {"Alice", 30}} sort.Sort(ps) for _, p := range ps { fmt.Printf("%s %d\n", p.Name, p.Age) } }
The output result is:
Jerry 20 Tom 25 Alice 30
Summary:
The Golang sort package provides a powerful sorting function that can sort different types of sequences. We can also use the sort.Interface interface to define custom types of sorting. The sorting algorithms provided by the sort package are optimized quick sort and heap sort, so they are more efficient. The entire sort package is simple to use and has clear logic. It is an indispensable package in Golang.
The above is the detailed content of Let's talk about how to use the Golang sort package. For more information, please follow other related articles on the PHP Chinese website!