Interface (you read that right, it’s called Interface)

This interface is the core interface of the SORT package through which sort packages are sorted

type Interface interface {
   Len() int // Returns the number of elements
   Less(i, j int) bool / / to compare
   Swap(i, j int) / / exchange
}
Copy the code

The sorting

Ints(), Float64s(), Strings()

These methods wrap Slice with classes that implement the corresponding type of Interface Interface, so we don’t need to implement the Interface Interface ourselves.

package main

import (
   "fmt"
   "sort"
)

func main(a) {
   arr := []int{1.3.5.3.4.2.1.2.3.6.8.3}
   sort.Ints(arr)
   fmt.Println(arr) // [1 1 2 2 3 3 3 4 5 6 8]
}
Copy the code

Sort()

This method needs to implement the parameters of the Interface, so we need to implement the Interface ourselves, for example:

package main

import (
   "fmt"
   "sort"
)

type IntSlice []int

func (x IntSlice) Len(a) int           { return len(x) }
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x IntSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }

func main(a) {
   arr := IntSlice{1.3.5.3.4.2.1.2.3.6.8.3}
   sort.Sort(arr)
   fmt.Println(arr) // [1 1 2 2 3 3 3 4 5 6 8]
}
Copy the code

Slice()

This method can implement only the Less() method of the Interface Interface, with the first argument being of type slice:

package main

import (
   "fmt"
   "sort"
)

func main(a) {
   arr := []int{1.3.5.3.4.2.1.2.3.6.8.3}
   sort.Slice(arr, func(i, j int) bool {
      return arr[i] < arr[j]
   })
   fmt.Println(arr) // [1 1 2 2 3 3 3 4 5 6 8]
}
Copy the code

Reverse()

Reverse, as in:

package main

import (
   "fmt"
   "sort"
)

type IntSlice []int

func (x IntSlice) Len(a) int           { return len(x) }
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x IntSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }

func main(a) {
   arr := IntSlice{1.3.5.3.4.2.1.2.3.6.8.3}
   sort.Sort(arr)
   fmt.Println(arr) // [1 1 2 2 3 3 3 4 5 6 8]
   sort.Sort(sort.Reverse(arr))
   fmt.Println(arr) // [8 6 5 4 3 3 3 2 2 1 1]
}
Copy the code

The internal implementation just changes the ij of Less() :

type reverse struct {
   Interface
}

func (r reverse) Less(i, j int) bool {
   return r.Interface.Less(j, i)
}

func Reverse(data Interface) Interface {
   return &reverse{data}
}
Copy the code

Stable (), SliceStable ()

Ensure that sorting is stable, i.e. the original positions of the same worthwhile elements do not change:

package main

import (
   "fmt"
   "sort"
)

type Item struct {
   Index int
   Value int
}

func main(a) {
   arr := []Item{{1.1}, {2.3}, {3.5}, {4.3}, {5.4}, {6.1}, {7.1}, {8.1}}
   fmt.Println(arr) / / [{1, 1} {2, 3} {5} 3 4 {3} {4} 5 6 {1} {1} 7 8 {1}]
   arrCopy := make([]Item, len(arr))
   copy(arrCopy, arr)

   // Unstable sort
   sort.Slice(arr, func(i, j int) bool {
      return arr[i].Value < arr[j].Value
   })
   [8 1] [6 1] [7 1] [4 3] [2 3
   fmt.Println(arr) / / [8 {1} {1, 1} {1} 6 7 {1} {4 3} {2, 3} {5, 4} {3} 5]

   // Stable sort
   sort.SliceStable(arrCopy, func(i, j int) bool {
      return arrCopy[i].Value < arrCopy[j].Value
   })
   fmt.Println(arrCopy) 6 / / [{1, 1} {1} {1} 7 8 {1} {2, 3} {3} 4 5 4} {5} {3]
}
Copy the code

Is it sorted

There are five sorted methods: IntsAreSorted(), Float64sAreSorted(), StringsAreSorted(), IsSorted(), and SliceIsSorted()

The first three do not need to implement their own Interface, IsSorted() needs to implement its own Interface(), and SliceIsSorted() can just implement Less()

package main

import (
   "fmt"
   "sort"
)

func main(a) {
   arr := []int{1.3.5.3.4.2.1.2.3.6.8.3}
   fmt.Println(sort.IntsAreSorted(arr)) // false
   sort.Ints(arr)
   fmt.Println(sort.IntsAreSorted(arr)) // true
}
Copy the code

search

The function searches for the subscript that should be inserted into an element of a sorted list

There are four methods: Search(), SearchInts(), SearchFloat64s(), and SearchStrings()

The first Search() needs to implement the Interface Interface, and the next three don’t

package main

import (
   "fmt"
   "sort"
)

func main(a) {
   arr := []int{1.3.5.3.4.2.1.2.3.6.8.3}
   sort.Ints(arr)
   fmt.Println(arr) // [1 1 2 2 3 3 3 4 5 6 8]
   fmt.Println(sort.SearchInts(arr, 3)) / / 4
   fmt.Println(sort.SearchInts(arr, 7)) / / 11
}
Copy the code