1, Integer
First of all, use the simplest example provided inside to sort the shaping
package main import ( "fmt" "sort" ) func main() { a := sort.IntSlice{2, 8, 3, 7, 9, 4, 1, 6} fmt.Println("Before sorting", a) sort.Sort(a) fmt.Println("After sorting", a) }
You'd better wonder what sort.IntSlice is. Click to view the source code. You can see that it is of type [] int and bound with four methods. The first three must be owned by the sorted object (that is, if you want to sort an object, you must implement these methods), but the sort package has been implemented for us. Then a Sort() method is bound
Based on this information, we can modify the previous example and directly call the Sort() method bound in the object instead of using the Sort() method provided in the sort package. The result remains unchanged.
func main() { a := sort.IntSlice{2, 8, 3, 7, 9, 4, 1, 6} fmt.Println("Before sorting", a) // sort.Sort(a) a.Sort() fmt.Println("After sorting", a) }
If we do not use the sort.IntSlice type provided inside, but implement one by ourselves, how to write it, directly copy the IntSlice implementation inside, and then change the name [wry smile. jpg]
package main import ( "fmt" "sort" ) type paixua []int func (x paixua) Len() int { return len(x) } func (x paixua) Less(i, j int) bool { return x[i] < x[j] } func (x paixua) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func (x paixua) Sort() { sort.Sort(x) } func main() { a := paixua{2, 8, 3, 7, 9, 4, 1, 6} fmt.Println("Before sorting", a) a.Sort() fmt.Println("After sorting", a) }
What are the functions of the three methods?
Len(): returns the length of the slice
Less(): Boolean value of the result of two value comparison
Swap(): used to swap two values
- Generally, we only need to modify the Less() method, because we can see that the previous sorting is in ascending order. Changing the less than sign to the greater than sign is in descending order
- There are also methods to reverse the order without modifying it yourself
What sort algorithm is used in it?
Looking at the source code, as shown in the figure below, you can find that the quickSort fast sorting algorithm is used
Interestingly, look at the back of the figure and find Ints, Float64s and Strings. In fact, it is also a kind of encapsulation. The usage is simpler, as follows
func main() { a := []int{5, 2, 7, 6, 4, 1, 3, 9} fmt.Println("Before sorting", a) sort.Ints(a) fmt.Println("After sorting", a) }
You also find intsareseported, float64sareseported and stringsareseported. It just returns whether a slice has been sorted in ascending order. Personally, it's chicken ribs.
Reverse order
At the same time, you can also find the Reverse method, which is used in Reverse order. The usage is as follows
func main() { a := []int{2, 8, 3, 7, 9, 4, 1, 6} fmt.Println("Before sorting", a) sort.Sort(sort.Reverse(sort.IntSlice(a))) fmt.Println("After sorting", a) }
After understanding the sorting of shaping, it is easy to understand other types of sorting, such as floating point and string
2, Floating point type
3, String type
It can be found that strings are sorted according to the first letter. If they are the same, they are sorted according to the second digit of the same two numbers. They are case sensitive and uppercase, which conforms to the rules of ascall code
4, Is it really a quick sort algorithm?
Find the sorting algorithm in the sort package and find four
insertionSort: insert sort
heapSort: heap sort
quickSort: quick sort
symMerge: merge sort
Returning to the quickSort method, I found that it will automatically choose to use the sorting algorithm
5, Stability
The package also states that using sort.Sort() is not stable
To be stable, use the Stable() function
You can use the method of sort.IntSlice {}
You can also use custom methods
However, it cannot be implemented using the method of sort.Ints(), because it uses sort.Sort()
// This is implemented in a custom way package main import ( "fmt" "sort" ) type paixua []int func (x paixua) Len() int { return len(x) } func (x paixua) Less(i, j int) bool { return x[i] < x[j] } func (x paixua) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func main() { a := paixua{2, 8, 3, 7, 9, 4, 1, 6} fmt.Println("Before sorting", a) sort.Stable(a) fmt.Println("After sorting", a) }
6, Structure type sorting
Sort package does not provide sort of struct type, but don't you think type paixuArr []paixu and type paixu []int are the same?
package main import ( "fmt" "sort" ) type paixu struct { Name string Id int } type paixuArr []paixu func (x paixuArr) Len() int { return len(x) } func (x paixuArr) Less(i, j int) bool { return x[i].Id < x[j].Id } func (x paixuArr) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func NewObj() paixuArr { return paixuArr{ {"a", 3}, {"b", 1}, {"c", 4}, {"d", 2}, } } func main() { a := NewObj() // Initialization data fmt.Println("Before sorting", a) sort.Sort(a) fmt.Println("After sorting", a) }