一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!


sort包的内容,以及使用

sort包提供了排序切片和用户自定义数据集以及相关功能的函数。

sort包主要针对[]int、[]float64、[]string、以及其他自定义切片的排序。

结构体
type IntSlice struct
type Float64Slice
type StringSlice
函数
func Ints(a []int)
func IntsAreSorted(a []int) bool
func SearchInts(a []int, x int) int
func Float64s(a []float64)
func Float64sAreSorted(a []float64) bool
func SearchFloat64s(a []float64, x float64) int
func SearchFloat64s(a []float64, x float64) bool
func Strings(a []string)
func StringsAreSorted(a []string) bool
func SearchStrings(a []string, x string) int
func Sort(data Interface)
func Stable(data Interface)
func Reverse(data Interface) Interface
func ISSorted(data Interface) bool
func Search(n int, f func(int) bool) int

接口 type interface

type Interface interface {
    Len() int           // Len方法返回集合中的元素个数
    Less(i, j int) bool // i>j,该方法返回索引i的元素是否比索引j的元素小
    Swap(i, j int)      // 交换i,j的值
}

实例

package main

import (
    "fmt"
    "sort"
)

type NewInts []uint

func (n NewInts) Len() int {
    return len(n)
}

func (n NewInts) Less(i, j int) bool {
    fmt.Println(i, j, n[i] < n[j], n)
    return n[i] < n[j]
}

func (n NewInts) Swap(i, j int) {
    n[i], n[j] = n[j], n[i]
}

func main() {
    n := []uint{1, 3, 2}
    sort.Sort(NewInts(n))
    fmt.Println(n)

}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
1 0 false [1 3 2]
2 1 true [1 3 2]
1 0 false [1 2 3]
[1 2 3]
结构体

三种结构体的方法都是一样的,只是分别针对int切片、float64切片、strings切片这三种不同的类型。

然后三种结果的都有五种公开方法

func (p xxxSlice) Len() int //切片长度
func (p xxxSlice) Less(i, j int) bool
func (p xxxSlice) Swap(i, j int)
func (p xxxSlice) Search(x xxx) int
// 这个和后面那个功能一样
func (p xxxSlice) Sort()
综合实例

[]float64:

package main

import (
    "fmt"
    "sort"
)

func main() {
    f := []float64{1.1, 4.4, 5.5, 3.3, 2.2}
    sort.Float64s(f)
    fmt.Printf("f: %v\n", f)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
f: [1.1 2.2 3.3 4.4 5.5]

[]int:

package main

import (
    "fmt"
)

func main() {
    f := []int{3, 5, 1, 2, 4}
    fmt.Printf("f: %v\n", f)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
f: [3 5 1 2 4]

string:

package main

import (
    "fmt"
    "sort"
)

func main() {
    ls := sort.StringSlice{
        "100",
        "42",
        "41",
        "3",
        "2",
    }
    fmt.Println(ls)
    sort.Strings(ls)
    fmt.Println(ls)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[100 42 41 3 2]
[100 2 3 41 42]
package main

import (
    "fmt"
    "sort"
)

func main() {
    ls := sort.StringSlice{
        "d",
        "ac",
        "c",
        "ab",
        "e",
    }
    fmt.Println(ls)
    sort.Strings(ls)
    fmt.Println(ls)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[d ac c ab e]
[ab ac c d e]
package main

import (
    "fmt"
    "sort"
)

func main() {
    ls := sort.StringSlice{
        "啊",
        "博",
        "次",
        "得",
        "饿",
        "周",
    }
    fmt.Println(ls)
    sort.Strings(ls)
    fmt.Println(ls)

    for _, v := range ls {
        fmt.Println(v, []byte(v))
    }
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[啊 博 次 得 饿 周]
[博 周 啊 得 次 饿]
博 [229 141 154]
周 [229 145 168]
啊 [229 149 138]
得 [229 190 151]
次 [230 172 161]
饿 [233 165 191]

复杂结构:[][]int:

package main

import (
    "fmt"
    "sort"
)

type testSlice [][]int

func (l testSlice) Len() int           { return len(l) }
func (l testSlice) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i][1] < l[j][1] }

func main() {
    ls := testSlice{
        {1, 4},
        {9, 3},
        {7, 5},
    }

    fmt.Println(ls)
    sort.Sort(ls)
    fmt.Println(ls)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[[1 4] [9 3] [7 5]]
[[9 3] [1 4] [7 5]]

复杂结构体:[]map[string]int [{"k":0}, {"k1":1}, {"k2":2}]:

package main

import (
    "fmt"
    "sort"
)

type testSlice []map[string]float64

func (l testSlice) Len() int           { return len(l) }
func (l testSlice) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i]["a"] < l[j]["a"] } // 按照"a"对应的值排序

func main() {
    ls := testSlice{
        {"a": 4, "b": 12},
        {"a": 3, "b": 11},
        {"a": 5, "b": 10},
    }

    fmt.Println(ls)
    sort.Sort(ls)
    fmt.Println(ls)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[map[a:4 b:12] map[a:3 b:11] map[a:5 b:10]]
[map[a:3 b:11] map[a:4 b:12] map[a:5 b:10]]

复杂结构体:[]struct:

package main

import (
    "fmt"
    "sort"
)

type People struct {
    Name string
    Age  int
}

type testSlice []People

func (l testSlice) Len() int           { return len(l) }
func (l testSlice) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i].Age < l[j].Age }

func main() {
    ls := testSlice{
        {Name: "n1", Age: 12},
        {Name: "n2", Age: 11},
        {Name: "n3", Age: 10},
    }

    fmt.Println(ls)
    sort.Sort(ls)
    fmt.Println(ls)
}

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
[{n1 12} {n2 11} {n3 10}]
[{n3 10} {n2 11} {n1 12}]